1 package com.freemindcafe.concurrency.sample7;
3 import java.util.ArrayList;
6 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.concurrent.CopyOnWriteArrayList;
12 public void calling_conditional_replace_will_work_only_if_the_state_has_not_been_changed_1()
throws Exception{
13 Map<Integer, List<Integer>> cache =
new ConcurrentHashMap<Integer, List<Integer>>();
14 List<Integer> l =
new ArrayList<>();
19 List<Integer> list = cache.get(1);
21 System.out.println(cache.replace(1, list, list));
26 List<Integer> list = cache.get(1);
28 if(!cache.replace(1, list, list)){
35 new Thread(r1).start();
36 new Thread(r2).start();
45 public void calling_conditional_replace_will_work_only_if_the_state_has_not_been_changed_2()
throws Exception{
46 Map<Integer, List<Integer>> cache =
new ConcurrentHashMap<Integer, List<Integer>>();
47 List<Integer> l =
new CopyOnWriteArrayList<Integer>();
52 List<Integer> list = cache.get(1);
54 System.out.println(cache.replace(1, list, list));
59 List<Integer> list = cache.get(1);
61 if(!cache.replace(1, list, list)){
68 new Thread(r1).start();
69 new Thread(r2).start();
78 public void calling_conditional_replace_will_work_only_if_the_state_has_not_been_changed_3()
throws Exception{
79 Map<Integer, Object> cache =
new ConcurrentHashMap<Integer, Object>();
80 cache.put(1,
new Object());
83 Object obj = cache.get(1);
84 System.out.println(cache.replace(1, obj,
new Object()));
89 Object obj = cache.get(1);
90 if(!cache.replace(1, obj,
new Object())){
97 new Thread(r1).start();
98 new Thread(r2).start();