简体   繁体   English

同步循环链表

[英]Synchronizing circular linked list

I am writing a program with 2 threads. 我正在编写一个具有2个线程的程序。 one in iterating a circular linked list . 一个在循环链表中的迭代。 the list has always a next element because the linked list is circular . 该列表始​​终具有下一个元素,因为链接列表是循环的。 another thread in modifying the list. 另一个线程在修改列表。 but I get the concurrentModificationException. 但是我得到了并发修改异常。 What can i do with it? 我该怎么办? Thanks 谢谢

From the following api docs : 从以下api文档中:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ConcurrentModificationException.html http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ConcurrentModificationException.html

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. 当不允许对对象进行同时修改时,检测到该对象的同时修改的方法可能会引发此异常。

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. 例如,通常不允许一个线程修改Collection而另一个线程对其进行迭代。 In general, the results of the iteration are undefined under these circumstances. 通常,在这些情况下,迭代的结果是不确定的。 Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. 如果检测到此行为,则某些Iterator实现(包括JRE提供的所有通用集合实现的实现)可能会选择抛出此异常。 Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future. 执行此操作的迭代器称为快速失败迭代器,因为它们会快速干净地失败,而不是在未来的不确定时间内冒任意,不确定的行为的风险。

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. 请注意,此异常并不总是表示对象已被其他线程同时修改。 If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. 如果单个线程发出违反对象约定的方法调用序列,则该对象可能会抛出此异常。 For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. 例如,如果线程在使用快速失败迭代器迭代集合时直接修改了集合,则迭代器将抛出此异常。

Don't use iterators in this case because that's where the exception comes from. 在这种情况下,请勿使用迭代器,因为这是异常的来源。

Advance the current element by simply setting currentElement = currentElement.next() or whatever it is called in your (infinite?) loop. 只需设置currentElement = currentElement.next()或在(无限?)循环中调用的任何元素,即可推进当前元素。

You might get into trouble when modifying the data structure concurrently should the list not be thread safe. 如果列表不是线程安全的,则同时修改数据结构时可能会遇到麻烦。

Assuming your implementation is threadsafe (which class are you using?) Iterators aren't inherently threadsafe. 假设您的实现是线程安全的(正在使用哪个类?),迭代器并不是天生的线程安全的。 They don't hold a lock on the List, so they can't keep other threads from modifying it. 他们没有在列表上保持锁,因此他们不能阻止其他线程对其进行修改。

If your CircularList synchronizes on itself (not sure what you're using, but it's common for threadsafe lists - otherwise iterators would basically always been non-threadsafe), you can work around this by doing: 如果您的CircularList自身进行同步(不确定您使用的是什么,但是对于线程安全列表是很常见的-否则迭代器基本上总是非线程安全的),您可以通过以下方法解决此问题:

synchronized(list) {
  Iterator i = list.iterator();
  doSomething(i);
}

See also: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#synchronizedList(java.util.List) 另请参阅: http : //docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#synchronizedList(java.util.List)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM