LinkedHashSet - synchronizing using Collections.synchronizedSet program in java


LinkedHashSet - synchronizing using Collections.synchronizedSet program.


Iterator on synchronizedSet won't be synchronized, so will have to synchronize set using synchronization block during iteration in java.


import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class LinkedHashSetSynchronizingExample {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          Set<String> linkedLinkedHashSet = new LinkedHashSet<String>();
          linkedLinkedHashSet.add("ankit");
          linkedLinkedHashSet.add("javaMadeSoEasy");
          // getting synchronized LinkedHashSet
          Set<String> synchronizedSet = Collections.synchronizedSet(linkedLinkedHashSet);
         
          // Iterator on synchronizedSet won't be synchronized, so will have to
          // synchronize set using synchronization block during iteration
          System.out.println("------- iterating on synchronized set -------");
          synchronized (synchronizedSet) {
                 Iterator<String> iterator = synchronizedSet.iterator();
                 while (iterator.hasNext()) {
                       System.out.println(iterator.next());
                 }
          }
   }
}
/*output
------- iterating on synchronized set -------
ankit
javaMadeSoEasy
*/



RELATED LINKS>


HashSet - iterate using iterator, Enumeration and enhanced for loop program


HashSet - making set unmodifiable using Collections.unmodifiableSet


CopyOnWriteArraySet - add, contains, remove, size methods program


No comments:

Post a Comment