comparison src/treecms/tree/util/CopyOnWriteTreeMap.java @ 25:c1e7ec6b3d44

commit
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Tue, 12 Jul 2011 14:39:35 +0900
parents 68021f7091e1
children
comparison
equal deleted inserted replaced
24:68021f7091e1 25:c1e7ec6b3d44
1 package treecms.tree.util; 1 package treecms.tree.util;
2 2
3 import java.io.IOException;
3 import java.util.Collections; 4 import java.util.Collections;
4 import java.util.Comparator; 5 import java.util.Comparator;
5 import java.util.LinkedList; 6 import java.util.LinkedList;
6 import java.util.Map; 7 import java.util.Map;
7 import java.util.Random; 8 import java.util.Random;
18 { 19 {
19 private volatile TreeNode<K,V> m_read; 20 private volatile TreeNode<K,V> m_read;
20 private volatile TreeNode<K,V> m_write; 21 private volatile TreeNode<K,V> m_write;
21 22
22 private final Object m_writeLock; 23 private final Object m_writeLock;
23 public static void main(String _args[]) throws InterruptedException 24 public static void main(String _args[]) throws InterruptedException, IOException
24 { 25 {
25 //final CopyOnWriteTreeMap<String,String> map = new CopyOnWriteTreeMap<String,String>(); 26 System.in.read();
26 final Map<String,String> map = Collections.synchronizedMap(new TreeMap<String,String>()); 27
28 final CopyOnWriteTreeMap<String,String> map = new CopyOnWriteTreeMap<String,String>();
29 //final Map<String,String> map = Collections.synchronizedMap(new TreeMap<String,String>());
27 final LinkedList<String> keys = new LinkedList<String>(); 30 final LinkedList<String> keys = new LinkedList<String>();
28 31
29 Random r = new Random(); 32 Random r = new Random();
30 for(int i = 0;i < 5000;i ++){ 33 for(int i = 0;i < 10000;i ++){
31 String str = Long.toHexString(r.nextLong()); 34 String str = Long.toHexString(r.nextLong());
32 map.put(str,str);
33 keys.add(str); 35 keys.add(str);
34 } 36 }
35 37
36 ExecutorService service = Executors.newFixedThreadPool(5); 38 ExecutorService service = Executors.newFixedThreadPool(5);
37 39
38 Callable<Object> task = new Callable<Object>(){ 40 Callable<Object> writer = new Callable<Object>(){
41 @Override
42 public Object call() throws Exception
43 {
44 long mill = System.currentTimeMillis();
45 for(String str : keys){
46 map.put(str,str);
47 }
48 System.out.println("Writer :"+ (System.currentTimeMillis() - mill));
49 return null;
50 }
51 };
52
53 Callable<Object> reader = new Callable<Object>(){
39 @Override 54 @Override
40 public Object call() throws Exception 55 public Object call() throws Exception
41 { 56 {
42 long mill = System.currentTimeMillis(); 57 long mill = System.currentTimeMillis();
43 for(String str : keys){ 58 for(String str : keys){
44 map.get(str); 59 map.get(str);
45 } 60 }
46 System.out.println(System.currentTimeMillis() - mill); 61 System.out.println("Reader :"+ (System.currentTimeMillis() - mill));
47 62
48 return null; 63 return null;
49 } 64 }
50 }; 65 };
51 66
52 service.submit(task); 67 service.submit(reader);
53 service.submit(task); 68 service.submit(writer);
54 service.submit(task); 69 service.submit(writer);
55 service.submit(task); 70 service.submit(writer);
56 service.submit(task); 71 service.submit(writer);
72 service.submit(writer);
57 73
58 service.shutdown(); 74 service.shutdown();
59 service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); 75 service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS);
60 76
61 for(String key : keys){ 77 System.in.read();
62 String val = map.get(key);
63 if(val == null){
64 System.out.println("val("+key+") is null");
65 }
66 }
67 } 78 }
68 79
69 public CopyOnWriteTreeMap() 80 public CopyOnWriteTreeMap()
70 { 81 {
71 m_read = m_write = null; 82 m_read = m_write = null;