O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Thread-Safe Iterators

When it returns the next value in a collection to a client, an iterator also returns control of the program. This opens the possibility that a client will take an action that will change the collection over which you are iterating. Consider a program that can update a list in one thread while another thread is iterating over the list:

package com.oozinoz.applications;
import java.util.*;
public class ShowConcurrentFor implements Runnable { private List list; protected static List upMachineNames() { return new ArrayList ( Arrays.asList ( new String[] { "Mixer1201", "ShellAssembler1301", "StarPress1401", "UnloadBuffer1501" } ) ); } protected void go() { list = Collections.synchronizedList( upMachineNames()); for (int i = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required