Safe vector copy : Collections Threads « Threads « Java





Safe vector copy

Safe vector copy
   

import java.util.Vector;

public class SafeVectorCopy {
  public static void main(String[] args) {
    Vector vect = new Vector();
    vect.addElement("Synchronization");
    vect.addElement("is");
    vect.addElement("important");

    String[] word;

    synchronized (vect) {
      int size = vect.size();
      word = new String[size];

      for (int i = 0; i < word.length; i++) {
        word[i] = (String) vect.elementAt(i);
      }
    }

    System.out.println("word.length" + word.length);
    for (int i = 0; i < word.length; i++) {
      System.out.println("[" + i + "]=" + word[i]);
    }
  }
}
           
         
    
    
  










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 collection operationSafe collection operation
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