Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,在单独的线程中从ArrayList中删除项_Java_Multithreading_Arraylist - Fatal编程技术网

在Java中,在单独的线程中从ArrayList中删除项

在Java中,在单独的线程中从ArrayList中删除项,java,multithreading,arraylist,Java,Multithreading,Arraylist,我有一个长的循环 在循环中,我有另一个循环要在arrayList上迭代(使用迭代器) 如果在数组列表中循环时满足一个条件,我将在传递当前数组列表项和当前迭代器时启动一个线程 一些示例代码: List<Test> testList = Collections.synchronizedList(new ArrayList<Test>()); //scanTests in a thread while(scanTests) { for(Iterator<Test

我有一个长的循环

在循环中,我有另一个循环要在arrayList上迭代(使用迭代器)

如果在数组列表中循环时满足一个条件,我将在传递当前数组列表项和当前迭代器时启动一个线程

一些示例代码:

List<Test> testList = Collections.synchronizedList(new ArrayList<Test>());

//scanTests in a thread
while(scanTests) {
    for(Iterator<Test> itr = this.testList.iterator(); itr.hasNext();) {
        Test test = itr.next();
        if (test.closed == true && !test.inUpdateThread) {
            test.inUpdateThread = true;
            UpdateThread urt = new UpdateThread(test, itr);
            new Thread(urt).start();
        }
    }
}

//scanSomethingElse is in another thread
while (scanSomethingElse) {
    //manipulating testList 
}
我有另一个长时间运行的循环(在另一个单独的线程中),它同时在数组列表上执行类似类型的处理(读、写等)

我的问题是,如果在另一个循环中,我正在迭代testList,那么这会反映来自另一个循环的更改吗


(如果您想知道为什么我要使用线程进行同步,这是用于远程资源和数据库交互的,我需要这个循环是恒定的、瞬时的,而不需要等待时间)。

为什么不使用类似于:

while (true) {
    for (int i = 0;i < testList.size(); i++) {
        Test test = testList.get(i);
        if (test.closed == true && !test.inUpdateThread) {
            test.inUpdateThread = true;
            UpdateThread urt = new UpdateThread(some other constructor here);
            new Thread(urt).start();
        }; 

    };
};
while(true){
对于(int i=0;i

//UpdateThread类有一个构造函数UpdateThread(测试、迭代器itr)。因此,只需修改它以适合此示例,或者发布它,我认为ki可以为您做到。

我认为如果您在迭代时删除元素,您将得到ConcurrentModificationException,请参见:否。这是一个常见错误。如果在迭代器中使用add()/remove()方法,则可以在迭代时删除/添加。迭代器维护一个“modcount”(修改计数)属性,它在每次操作时更新该属性。如果修改它。remove(),则modcount将增加。如果您列出.remove(),则modcount不会增加,因此在下一个it.next()时,您会得到此异常。我认为这里的问题是迭代器本身不是线程safe@adenoyelle如果该项被另一个线程删除,则肯定不会通过迭代器删除。remove()-因此将发生异常。将迭代器传递给该线程。@lcplussplus:能否澄清UpdateThread中迭代器的用法?IMHO,在迭代器上循环,同时将其传递给另一个线程,该线程也可以更新迭代器,这是一个严重的设计缺陷;在UpdateThread中,而不是传递迭代器?是testList.remove(test);例如,将从列表中删除测试。我只想在迭代时删除当前的“测试”项,我只能确定是否要在UpdateThread中删除它。因此UpdateThread构造函数应该类似于UpdateThread(测试,列表),这样您就可以调用list.remove(测试)在UpdateThread类中,我的印象是,对arrayList使用迭代器是在遍历该项时删除该项的唯一方法。
while (true) {
    for (int i = 0;i < testList.size(); i++) {
        Test test = testList.get(i);
        if (test.closed == true && !test.inUpdateThread) {
            test.inUpdateThread = true;
            UpdateThread urt = new UpdateThread(some other constructor here);
            new Thread(urt).start();
        }; 

    };
};