尽管使用同步来对集合进行分类
我尝试使用螺纹方法对集合进行排序,其中方法单独调用比较器类,
public class ThreadedSort{
public ThreadedSort(){
ThreadedIDSort idSort=new ThreadedIDSort();
ThreadedNameSort nameSort=new ThreadedNameSort();
ThreadedSalarySort salarySort=new ThreadedSalarySort();
ExecutorService threadExecutor=Executors.newCachedThreadPool();
threadExecutor.execute(idSort);
threadExecutor.execute(nameSort);
threadExecutor.execute(salarySort);
}
}
每个螺纹方法看起来都如下:
public class ThreadedIDSort implements Runnable, EmployeeInterface {
public synchronized void run(){
employeeList.sort(new IDComparator());
}
}
ID隔离器类如下:
public class IDComparator implements Comparator<Employee> {
@Override
public int compare(Employee a, Employee b) {
return a.getID()-b.getID();
}
}
雇员是具有属性名称,ID,薪金和帖子的对象的列表:
ArrayList<Employee> employeeList=new ArrayList<>();
尽管我在运行方法之前添加了同步,编辑列表仍然给出contrentModificationException,
Exception in thread "pool-2-thread-2" Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList.sort(ArrayList.java:1723)
at Client.Sort.Threaded.ThreadedNameSort.run(ThreadedNameSort.java:9)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:831)
我尝试使用线程立即使用名称,帖子和ID进行排序。
I tried to sort the collection using threaded methods where the methods call comparator classes individually
public class ThreadedSort{
public ThreadedSort(){
ThreadedIDSort idSort=new ThreadedIDSort();
ThreadedNameSort nameSort=new ThreadedNameSort();
ThreadedSalarySort salarySort=new ThreadedSalarySort();
ExecutorService threadExecutor=Executors.newCachedThreadPool();
threadExecutor.execute(idSort);
threadExecutor.execute(nameSort);
threadExecutor.execute(salarySort);
}
}
Each threaded method looks like this:
public class ThreadedIDSort implements Runnable, EmployeeInterface {
public synchronized void run(){
employeeList.sort(new IDComparator());
}
}
The ID Compartator class is as follows:
public class IDComparator implements Comparator<Employee> {
@Override
public int compare(Employee a, Employee b) {
return a.getID()-b.getID();
}
}
The employeeList is a list of objects having attributes name, id, salary and post:
ArrayList<Employee> employeeList=new ArrayList<>();
Though I added synchronized before run method, editing the list still gives ConcurrentModificationException
Exception in thread "pool-2-thread-2" Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList.sort(ArrayList.java:1723)
at Client.Sort.Threaded.ThreadedNameSort.run(ThreadedNameSort.java:9)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:831)
I am trying to sort using name, post and ID all at once using threads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Rob Spoor解释了为什么
ConcurrentModificationException
不一定与线程有关。但是,您应该知道这一点:同步
关键字在run()
方法中没有效果。当您编写同步方法时,
这与您在示例中编写的情况相同
,您提交给执行程序服务的三个任务中的每个任务都是不同的
运行
对象。关键字<代码>此是指三个run()
方法中的每个对象。在三个不同对象上同步的三个不同线程与无同步相同。仅当线程在相同对象上同步时,同步才有意义。规则是,没有两个线程可以同时同步在相同的对象上同步。
Rob Spoor explained why
ConcurrentModificationException
isn't necessarily about threading. But also, you should know this:The
synchronized
keyword on therun()
method in your example has no effect.When you write a synchronized method,
That's the same as if you wrote,
In your example, each of the three tasks that you submit to the executor service is a different
Runnable
object. The keyword,this
, refers to a different object in each of the threerun()
methods. Three different threads synchronizing on three different objects is the same as no synchronization at all.Synchronization is only meaningful when the threads synchronize on the same object. The rule is, no two threads will ever be allowed to synchronize on the same object at the same time.
confurrentModification
也可以在单线螺纹应用程序中发生。当收集既修改又迭代时就会发生。这可能与以下内容一样简单:请注意,所谓的并发集合来自
java.util.concurrent
,已设计用于支持这一点,但大多数(全部?)来自java .util
即使您应用同步也存在相同的问题。不查看
雇主
的完全是IDCOMPARATOR
做什么,很难说出为什么在这种特定情况下抛出例外情况。A
ConcurrentModification
can also occur in single threaded applications. They occur when a collection is both modified and iterated over. That can be as simple as the following:Note that the so-called concurrent collections, from
java.util.concurrent
, have been designed to support this, but most (all?) fromjava.util
have the same issue, even if you apply synchronisation.Without seeing what
employeeList
is exactly, and whatIDComparator
does, it's difficult to say why the exception is thrown in this specific case.该问题是通过同时运行不同的线程来修改相同的“雇主”。
选择之一是克隆雇员,并分配单独的排序列表。
例子:
The issue is with same 'employeeList' being modified by concurrently running different threads.
One of the options is to clone the employeeList and assign separate list for sorting.
Example: