Thursday, February 25, 2016

Synchronized Array List, Hash Map and Hash Set

How to use Synchronized List, Map and Set ?

public class SynchronizedMapArraySet {

      public static void main(String[] args) throws InterruptedException {
            System.out.println("--Synchronized Array List(Order is                     maintained, Array)--");
            // unSynchronized Array List
            ArrayList<String> list = new ArrayList<String>();
            list.add("Paras");
            list.add("Chawla");
            list.add("Aman");
            list.add("Deep");

            // Synchronized Array List
            List<String> synchronizedList =                                           Collections.synchronizedList(list);

            synchronized (synchronizedList) {
                  Iterator<String> it = synchronizedList.iterator();
                  while (it.hasNext()) {
                        System.out.println(it.next());
                  }
            }

            System.out.println("------Synchronized Hash Set(No order is               maintained)-----");
            // unSynchronized Hash Set
            Set<String> set = new HashSet<>();
            set.add("Paras");
            set.add("Chawla");
            set.add("Aman");
            set.add("Deep");

            // Synchronized Hash Set
            Set<String> synchronizedSet =                                             Collections.synchronizedSet(set);
            synchronized (synchronizedSet) {
                  Iterator<String> it = synchronizedSet.iterator();
                  while (it.hasNext()) {
                        System.out.println(it.next());
                  }
            }

            System.out.println("------Synchronized Hash Map(No order is               maintained)-----");
            // unSynchronized Hash Map
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("Paras", 1);
            map.put("Chawla", 2);
            map.put("Aman", 3);
            map.put("Bedwal", 4);
            Map<String, Integer> synchronizedMap =                                     Collections.synchronizedMap(map);
            Set<String> mapSet = synchronizedMap.keySet();

            synchronized (synchronizedMap) {
                  Iterator<String> it = mapSet.iterator();
                  while (it.hasNext()) {
                        System.out.println(it.next());
                  }
            }

            Collection<Integer> mapValue = synchronizedMap.values();
            synchronized (synchronizedMap) {
                  Iterator<Integer> it = mapValue.iterator();
                  while (it.hasNext()) {
                        System.out.println(it.next());
                  }
            }

            final List<Integer> list1 = Collections.synchronizedList(new              ArrayList<Integer>());
           
            Thread t1 = new Thread(new Runnable() {
                  @Override
                  public void run() {
                        for (int i = 0; i < 500; i++) {
                              list1.add(i);
                        }
                  }

            });

            Thread t2 = new Thread(new Runnable() {

                  @Override
                  public void run() {
                        for (int i = 0; i < 500; i++) {
                              list1.add(i);
                        }
                  }
            });
            t1.start();
            t2.start();
            Thread.currentThread().sleep(3000);
            System.out.println("Using Synchronized Array List , its thread             safe- Size is"+list1.size());
      }
}

Output :

----Synchronized Array List(Order is maintained, Array)-----
Paras
Chawla
Aman
Deep
------Synchronized Hash Set(No order is maintained)-----
Aman
Chawla
Paras
Deep
------Synchronized Hash Map(No order is maintained)-----
Aman
Chawla
Paras
Bedwal
3
2
1
4

Using Synchronized Array List , its thread safe- Size is 1000


No comments:

Post a Comment