Safe collection operation : Collections Threads « Threads « Java






Safe collection operation

Safe collection operation
   


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class SafeCollectionIteration extends Object {
  public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Iterators");
    wordList.add("require");
    wordList.add("special");
    wordList.add("handling");

    synchronized (wordList) {
      Iterator iter = wordList.iterator();
      while (iter.hasNext()) {
        String s = (String) iter.next();
        System.out.println("found string: " + s + ", length="
            + s.length());
      }
    }
  }
}
           
         
    
    
  








Related examples in the same category

1.Java 1.5 (5.0) new features: PriorityQueueJava 1.5 (5.0) new features: PriorityQueue
2.Safe list copySafe list copy
3.Safe vector copySafe vector copy
4.Java 1.5 (5.0) new feature: collection and thread
5.Java Thread Performance: Collection Test
6.Java Thread Performance: AtomicTest
7.Rhyming WordsRhyming Words
8.Communicate between threads using a Queue
9.Using a Thread-Local Variable
10.A work queue is used to coordinate work between a producer and a set of worker threads.
11.Return a value from a thread.
12.A multithreaded queue used for implementing producer-consumer style threading patternsA multithreaded queue used for implementing producer-consumer style threading patterns
13.Customized java.util.ArrayList: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
14.Customized java.util.HashMap: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
15.Customized java.util.TreeMap: operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes.
16.Current set