简体   繁体   English

线程“main”java.util.ConcurrentModificationException中的异常

[英]Exception in thread “main” java.util.ConcurrentModificationException

When I run the below code, I get an exception. 当我运行以下代码时,我得到一个例外。 I searched but couldn't find any solution. 我搜索但找不到任何解决方案。

Exception in thread "main" java.util.ConcurrentModificationException
     at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
     at java.util.HashMap$KeyIterator.next(Unknown Source)
     at com.aybits.software.linkgrabber.Grabber.main(Grabber.java:45)

Line number 45 is for(String linkFromCollection : linksList){ 第45行用于(String linkFromCollection:linksList){

public class Grabber {

static String url;
Document doc;
static Set<String> linksList = new HashSet<String>();
String matchingString ="java2s.com/Code";
static boolean isCrawling = true;
static int STOP_WATCH = 0;

public Grabber(String url){
    Grabber.url = url;
}

public void grabLinks(String urlToCrawl) throws IOException{
    doc = Jsoup.connect(urlToCrawl).timeout(20 * 1000).get();
    Elements links = doc.select("a[href]");

    for (Element link : links) {
        //print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
        if(link.attr("abs:href").toString().contains(matchingString)){
            if(!linksList.contains(link.attr("abs:href").toString())){
                System.out.println("Added - " + link.attr("abs:href"));
                linksList.add(link.attr("abs:href").toString());
            }
        }
    }
}

public static void main(String[] args) throws IOException {
    Grabber app = new Grabber("http://java2s.com");
    app.grabLinks(url);

    while(isCrawling){
        for(String linkFromCollection : linksList){
            app.grabLinks(linkFromCollection);

            if(linksList.contains(linkFromCollection)){
                STOP_WATCH += 5;
                System.out.println("STOP_WATCH IS " + STOP_WATCH);
            }else{
                STOP_WATCH -= 1;
                System.out.println("STOP_WATCH IS " + STOP_WATCH);
            }

            if(STOP_WATCH >= 100){
                isCrawling = false;
                System.out.println("STOP_WATCH IS " + STOP_WATCH);
            }
        }


    }
    ICVSWrite writer = new ICVSWrite();

    String[] strArray = (String[]) linksList.toArray();
    writer.write(strArray);

}

}

The line 这条线

linksList.add(link.attr("abs:href").toString());

modifies the linksList collection while you are iterating over it. 在迭代它时修改linksList集合。 The next time through the for loop in main , Java calls next on the collection, sees that the collection has been modified, and throws the exception. 下一次通过main循环中的for循环,在集合上next调用Java,看到集合已被修改,并抛出异常。

When you are doing an enhanced for loop, you cannot add to or remove from the collection. 当您执行增强的for循环时,无法添加到集合或从集合中删除。

You cannot call add on a Collection while looping over it. 循环时,不能在Collection上调用add Here: 这里:

for (Element link : links) {        
    if(...){
        if(...){
            ...
            linksList.add(link.attr("abs:href").toString());
                      ^^^ <- here
        }
    }
}

You call grabLinks method from your main method from within a loop over linksList : 你叫grabLinks从你的方法main方法从环比内linksList

for(String linkFromCollection : linksList) {
   app.grabLinks(linkFromCollection);       

You have to add your items to another Collection and then copy them over after. 您必须将您的项目添加到另一个Collection ,然后将其复制。

What had me puzzled for a little while was why the exception was coming from a HashMap as I had assumed that linksList was a List - obviously it's a Set . 令我困惑的是,为什么异常来自HashMap因为我假设linksList是一个List - 显然它是一个Set Not the best name in the world. 不是世界上最好的名字。

This should help . 这应该有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 线程“main”中的异常 java.util.ConcurrentModificationException - Java - Exception in thread “main” java.util.ConcurrentModificationException ListIterator 抛出“线程“main”中的异常 java.util.ConcurrentModificationException”Java - ListIterator throws "Exception in thread "main" java.util.ConcurrentModificationException" Java 如何在非线程方法“线程“ main”中的异常” java.util.ConcurrentModificationException”中解决此问题 - how to solve this in non thread method “Exception in thread ”main“ java.util.ConcurrentModificationException” 举例来说,“线程“ Thread-2”中的异常java.util.ConcurrentModificationException” - Ecplise, “Exception in thread ”Thread-2“ java.util.ConcurrentModificationException” 线程“JavaFX 应用程序线程”中的异常 java.util.ConcurrentModificationException - Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException 数独求解器在其迭代器上的线程“主”java.util.ConcurrentModificationException 中出现异常 - Sudoku solver with Exception in thread “main” java.util.ConcurrentModificationException on its Iterator 线程“ main”中的异常java.util.ConcurrentModificationException-进行设置并删除 - Exception in thread “main” java.util.ConcurrentModificationException - go through the set and delet Java:线程“ AWT-EventQueue-0”中的异常java.util.ConcurrentModificationException - Java: Exception in thread “AWT-EventQueue-0” java.util.ConcurrentModificationException 致命异常:主进程 PID:20793 java.util.ConcurrentModificationException - FATAL EXCEPTION: main Process PID: 20793 java.util.ConcurrentModificationException 致命异常:Android中的Thread-92(java.util.ConcurrentModificationException) - FATAL EXCEPTION: Thread-92 (java.util.ConcurrentModificationException) in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM