Java Reference
In-Depth Information
// ... as before
}
protected void go()
{
list = Collections.synchronizedList(
upMachineNames());
Iterator i = list.iterator();
int j = 0;
while (i.hasNext())
{
j++;
if (j == 1)
{ // simulate wake-up
new Thread(this).start();
}
System.out.println(i.next());
}
}
public void run()
{
list.add(0, "Fuser1101");
}
public static void main(String[] args)
{
new ShowConcurrentIterator().go();
}
}
With this code in place, the program crashes :
> java com.oozinoz.applications.ShowConcurrentIterator
Mixer1201
ShellAssembler1301
StarPress1401
UnloadBuffer1501
Exception in thread "main"
java.util.ConcurrentModificationException
...
The program crashes because the iterator object detects that the list has changed during the
iteration. You don't need to create a new thread to show this behavior. However, a
multithreaded application is much more likely to accidentally modify a list while an iterator is
traversing it. In a large application, you may have little control over, or even knowledge of,
the number of threads running in the application and their various roles. In such an
application, you have to avoid presenting a corrupt representation of a collection, but you
can't afford to crash your application as a preventive measure.
Two main approaches provide safe iteration over a collection in a multithreaded application.
Both approaches involve the use of an object—sometimes called a mutex —that is shared by
threads that vie for control of the object's monitor.
In one approach, your design can require that all threads gain control of the mutex monitor
before accessing the collection:
Search WWH ::




Custom Search