首页 \ 问答 \ 我无法弄清楚这个ConcurrentModificationException(I can't figure out this ConcurrentModificationException)

我无法弄清楚这个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 sortmethod.
  • 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
更新时间:2022-08-24 11:08

最满意答案

命令行工具最适合静态归档,因为所有内容都作为单个二进制文件分发。 看看Realm,我没有看到有一个静态存档选项。 他们有一个iOS静态框架,我为macOS编译,但这不是你想要的。 您可能想尝试更多地使用Realm的源代码来查看是否可以使用它来生成静态存档。

同时,作为一种解决方法 ,您需要告诉Xcode在运行时在哪里找到dylib并在某处安装它们。

  1. 在Build Settings中,转到“Runpath Search Paths”并添加“ @rpath ”。
  2. 在Build Phases中,在Copy Files下,单击+按钮并从项目中添加Realm.framework和RealmSwift.framework。
  3. 因为Realm是使用较旧版本的Swift编译的,所以您还需要在Build Settings中指定“Use Legacy Swift Language Version”。

这将使您的项目构建并找到Realm库,但现在它将无法找到libswiftCore.dylib。 这是因为通常命令行工具与Swift库静态链接,但只要添加框架/ dylib,链接器就不再包含静态版本。

  1. 返回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.

  1. In your Build Settings, go down to "Runpath Search Paths" and add "@rpath".
  2. In Build Phases, under Copy Files, click the + button and add both Realm.framework and RealmSwift.framework from your project.
  3. 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.

  1. 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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。