我无法弄清楚这个ConcurrentModificationException(I can't figure out this ConcurrentModificationException)
我觉得我的Java生锈了,所以我尝试了一个简单的问题:使用链接列表进行合并排序,只返回唯一值。 以下是我的第一次尝试之一。 这对我来说很有意义。 问题是,无论我做了什么,我总是得到一个
ConcurrentModificationException
。我尝试过的事情包括:
- 使块或方法
synchronized
。- 用
sort
方法中的迭代器替换LinkedLists。- 在单独的变量中跟踪列表的大小(该部分留在其中)。
没有任何效果。
ConcurrentModificationsException
移动到其他地方但它仍然存在。 我真的不确定为什么。 我怎样才能解决这个问题?在此特定尝试中,异常发生在调用
addAll
。 我把列表中的所有操作都放在了自己的块中,但是Java显然并不关心。package com.regularoddity.dotcloud; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class Merge { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> myList = new LinkedList<Integer>(); myList.add(2); myList.add(9); myList.add(1); myList.add(3); myList.add(8); myList.add(7); myList.add(1); myList = sort(myList); Iterator<Integer> lit = myList.iterator(); while(lit.hasNext()) System.out.printf("%s ", lit.next()); } private static List<Integer> merge(List<Integer> list1, List<Integer> list2) { List<Integer> result = new LinkedList<Integer>(); int size1 = list1.size(); int size2 = list2.size(); { while (size1 > 0 && size2 > 0) { if (list1.get(0) == list2.get(0)) { result.add(list1.remove(0)); list2.remove(0); size1--; size2--; } else if (list1.get(0) <= list2.get(0)) { result.add(list1.remove(0)); size1--; } else { result.add(list2.remove(0)); size2--; } } } result.addAll(list1); result.addAll(list2); return result; } private static List<Integer> sort(List<Integer> list) { int lsize = list.size(); if (lsize <= 1) return list; int pivot = lsize / 2; List<Integer> list1 = list.subList(0, pivot); List<Integer> list2 = list.subList(pivot, lsize); list1 = sort(list1); list2 = sort(list2); return merge(list1, list2); } }
让我感到困惑的是我从未明确地与列表进行交互。 必须有一个隐式迭代,但我似乎无法将其分解出来。
编辑:我没有包括堆栈跟踪,因为我(愚蠢地)认为,因为它为每次修复尝试而改变,所以它没有用。 但是,实际上,它没有太大变化。 这是当前实现的堆栈跟踪:
Exception in thread "main" java.util.ConcurrentModificationException at java.util.SubList.checkForComodification(AbstractList.java:752) at java.util.SubList.size(AbstractList.java:625) at com.regularoddity.dotcloud.Merge.merge(Merge.java:30) at com.regularoddity.dotcloud.Merge.sort(Merge.java:64) at com.regularoddity.dotcloud.Merge.sort(Merge.java:62) at com.regularoddity.dotcloud.Merge.main(Merge.java:23)
com.regularoddity.dotcloud.Merge.main(Merge.java:23)
I was feeling my Java was rusty, so I tried a simple problem: a merge sort with Linked Lists that only returns unique values. Below was one of my first attempts. It made sense to me. The problem was, no matter what I did, I always got a
ConcurrentModificationException
.Things I've tried include:
- Making blocks or methods
synchronized
.- Replacing the LinkedLists with Iterators inside the
sort
method.- Keeping track of the size of the lists in separate variables (that part was left in).
Nothing worked. The
ConcurrentModificationsException
moved to other places but it remained in place. I'm really not sure why. How can I fix this?In this particular attempt, the exception occurs at the call to
addAll
. I put all the operations on the lists inside their own block, but Java obviously doesn't care.package com.regularoddity.dotcloud; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class Merge { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> myList = new LinkedList<Integer>(); myList.add(2); myList.add(9); myList.add(1); myList.add(3); myList.add(8); myList.add(7); myList.add(1); myList = sort(myList); Iterator<Integer> lit = myList.iterator(); while(lit.hasNext()) System.out.printf("%s ", lit.next()); } private static List<Integer> merge(List<Integer> list1, List<Integer> list2) { List<Integer> result = new LinkedList<Integer>(); int size1 = list1.size(); int size2 = list2.size(); { while (size1 > 0 && size2 > 0) { if (list1.get(0) == list2.get(0)) { result.add(list1.remove(0)); list2.remove(0); size1--; size2--; } else if (list1.get(0) <= list2.get(0)) { result.add(list1.remove(0)); size1--; } else { result.add(list2.remove(0)); size2--; } } } result.addAll(list1); result.addAll(list2); return result; } private static List<Integer> sort(List<Integer> list) { int lsize = list.size(); if (lsize <= 1) return list; int pivot = lsize / 2; List<Integer> list1 = list.subList(0, pivot); List<Integer> list2 = list.subList(pivot, lsize); list1 = sort(list1); list2 = sort(list2); return merge(list1, list2); } }
The thing that baffles me the most is that I never explicitly interate over the list. There must be an implicit iteration going on, but I don't seem to be able to factor it out.
EDIT: I didn't include the stacktrace because I (foolishly) thought that as it changed for every attempt at a fix, it would not be useful. But, really, it doesn't change much. Here's the stacktrace for the current implementation:
Exception in thread "main" java.util.ConcurrentModificationException at java.util.SubList.checkForComodification(AbstractList.java:752) at java.util.SubList.size(AbstractList.java:625) at com.regularoddity.dotcloud.Merge.merge(Merge.java:30) at com.regularoddity.dotcloud.Merge.sort(Merge.java:64) at com.regularoddity.dotcloud.Merge.sort(Merge.java:62) at com.regularoddity.dotcloud.Merge.main(Merge.java:23)
com.regularoddity.dotcloud.Merge.main(Merge.java:23)
原文:https://stackoverflow.com/questions/16781136
最满意答案
命令行工具最适合静态归档,因为所有内容都作为单个二进制文件分发。 看看Realm,我没有看到有一个静态存档选项。 他们有一个iOS静态框架,我为macOS编译,但这不是你想要的。 您可能想尝试更多地使用Realm的源代码来查看是否可以使用它来生成静态存档。
同时,作为一种解决方法 ,您需要告诉Xcode在运行时在哪里找到dylib并在某处安装它们。
- 在Build Settings中,转到“Runpath Search Paths”并添加“
@rpath
”。- 在Build Phases中,在Copy Files下,单击+按钮并从项目中添加Realm.framework和RealmSwift.framework。
- 因为Realm是使用较旧版本的Swift编译的,所以您还需要在Build Settings中指定“Use Legacy Swift Language Version”。
这将使您的项目构建并找到Realm库,但现在它将无法找到libswiftCore.dylib。 这是因为通常命令行工具与Swift库静态链接,但只要添加框架/ dylib,链接器就不再包含静态版本。
- 返回Build Phases,Copy Files,并添加以下内容:
libswiftObjectiveC.dylib libswiftIOKit.dylib libswiftFoundation.dylib libswiftDispatch.dylib libswiftDarwin.dylib libswiftCoreGraphics.dylib libswiftCore.dylib
您可以在Xcode安装中找到它们./Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/lib/swift/macosx/
警告:请记住,您需要使用命令行工具分发框架和dylib,并且它们需要与工具位于同一目录中。 您可以通过指定不同的运行路径将它们放在系统上的其他位置,但您仍然需要使用工具分发它们。
关于.app软件包的好处是,它为您提供了放置这些内容的位置,用户只需将其拖放即可安装它。 如果您可以获得Realm的静态存档版本,则可以将所有内容分发到一个二进制文件中。
Command-line tools are best with static archives because everything is distributed as a single binary. Looking at Realm, I don't see that there is a static archive option. They do have an iOS static framework that I got compiling for macOS but that's not quite what you want. You might want to try playing with Realm's source a bit more to see if you can get it to produce a static archive.
In the mean time, as a workaround, you'll need to tell Xcode where to find the dylibs at runtime and also to install them somewhere.
- In your Build Settings, go down to "Runpath Search Paths" and add "
@rpath
".- In Build Phases, under Copy Files, click the + button and add both Realm.framework and RealmSwift.framework from your project.
- Because Realm is compiled with an older version of Swift, you also need to specify "Use Legacy Swift Language Version" in Build Settings.
That will get your project building and finding the Realm libraries but now it will fail to find libswiftCore.dylib. That's because normally command-line tools are statically linked with the Swift library but as soon as you add a framework/dylib, the linker no longer includes the static version.
- Go back to Build Phases, Copy Files, and add the following:
libswiftObjectiveC.dylib libswiftIOKit.dylib libswiftFoundation.dylib libswiftDispatch.dylib libswiftDarwin.dylib libswiftCoreGraphics.dylib libswiftCore.dylib
You can find them inside your Xcode installation and then ./Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/lib/swift/macosx/
WARNING: Keep in mind that you will need to distribute the frameworks and the dylibs with your command-line tool and they will need to be in the same directory as the tool. You can put them somewhere else on the system by specifying a different runpath but you'll still need them distributed with your tool.
The nice thing about a .app bundle is that it gives you a place to put this stuff and users can just drag-and-drop it to install it. If you could get a static archive version of Realm, you could distribute everything in one binary.
相关问答
更多-
正如评论中所建议的那样,您可以使用git pre-commit挂钩在提交时生成README.rst文件。 你可以使用现有的工具,如cog,或者你可以用bash做一些简单的事情。 例如,创建一个RST“模板”文件: README.rst.tmpl Test Git pre-commit hook project -------------------------------- >>> INSERTION POINT FOR HELP OUTPUT <<< 的.git /钩/预提交 # Sensible t ...
-
解决这个问题的简单方法(假设你的PATH上有dotnet)就是创建一个简单的CMD脚本来启动程序。 例 CMD所在的目录(预计位于Path上): C:\Users\myuser\Scripts 部署到的目录: C:\Users\myuser\Scripts\RandomImage CMD文件的名称: RandomImage.cmd CMD文件的内容: @dotnet "%~dp0\RandomImage\RandomImage.dll" %* %~dp0 是脚本本身的路径 %* 所有参数传递给DLL 你可 ...
-
执行DMT.exe /? 将显示从命令行运行DMT的帮助。 我假定你的环境意味着你想要连接的实例,即飞行员,测试,生活等。 这是作为ConnectionUrl一部分完成的: DMT.exe -ConnectionUrl=”net.tcp://localhost/Live” -Import=”Part” -Source=”C:\Imports\Part.csv” -Add=True -Update=True -user=manager -pass=manager 您可以从客户端配置文件夹中的.sysconf ...
-
你试过把它关掉再打开吗? 抱歉,但是,实际上,请关闭控制台并再次打开它,以便更新路径。 它可能有所帮助。 Managed to work out what the issue was. Whoever set up the jenkins slave in the first place created the PATH in the config page, so the PATH it was using had nothing to do with the path on the actual bui ...
-
对于命令行选项,我们必须使用: -framework frameworkname For command line option we have to use: -framework frameworkname
-
无法在Phpstorm中为作曲家添加命令行工具支持(Unable to add command line tool support for the composer in Phpstorm)[2022-04-04]
PHPStorm应该知道本地安装的PHP,它所需要的只是包含Composer的PHP源代码的composer.phar文件。 你给PHPStorm看起来像一个Windows批处理文件,它检测在Windows shell中调用的某些方面(如CMD与Cygwin),然后THEN使用composer.phar的路径调用PHP。 这不是你可以直接给PHP的PHP源代码,就像PHPStorm那样。 仅在PHPStorm中配置phar文件的路径,而不是该批处理文件。 或者,您可以通过PHPStorm GUI简单地添加C ... -
如果我正确理解您的问题,您希望管理主项目使用的API框架项目。 快车道(恕我直言)是关于建造和管理建筑文物。 管理项目中的依赖项是另一回事。 为此,我知道有两种选择: Cocoapods - 长期以来唯一的选择。 基于Ruby的工具。 通过向项目添加构建和XCConfigs来管理依赖项。 这是通过重写您的项目文件来实现的。 在构建时重建所有依赖项。 我的意见 - 我从未成为粉丝。 我不喜欢你真的需要知道Ruby使用它,它侵入你的项目的方式并强制它建立你的想法。 迦太基 - 更新的选择。 OS X本机工具。 ...
-
命令行工具最适合静态归档,因为所有内容都作为单个二进制文件分发。 看看Realm,我没有看到有一个静态存档选项。 他们有一个iOS静态框架,我为macOS编译,但这不是你想要的。 您可能想尝试更多地使用Realm的源代码来查看是否可以使用它来生成静态存档。 同时,作为一种解决方法 ,您需要告诉Xcode在运行时在哪里找到dylib并在某处安装它们。 在Build Settings中,转到“Runpath Search Paths”并添加“ @rpath ”。 在Build Phases中,在Copy Fil ...
-
假设你在谈论javac的命令行调用,你所说的是“我可以提供库作为javac的参数来满足编译期间的需求”。 man javac顶级条目说 -classpath classpath Sets the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -class- path ...
-
使用命令行将库/框架添加到XCode项目?(Adding libraries/Frameworks to Xcode project using the command line?)[2022-05-22]
该项目可以处理框架: https : //github.com/kronenthaler/mod-pbxproj 只需将其添加为普通文件,它将找出正确的类型以及如何设置所有内容(即,将其添加到链接库阶段 - 在使用之前,您仍然需要导入标题,当然) 。 // libFilePath: Path to the framework frameworkGroup = project.get_or_create_group('Frameworks') project.add_file(libF ...