package com.maxthon;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class TestMap {

/**
* @param args
*/
public static void main(String[] args) {
//final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
final Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
final Hashtable<String, String> table = new Hashtable<String, String>();
final Runnable run2 = new Runnable() {
@Override
public void run() {
Set<Entry<String, String>> set = table.entrySet();

synchronized (this) {
int i = 0;
for (Entry<String, String> en : set) {
System.out.println(en.getKey() + ": " + en.getValue());
i++;
}
System.out.println("the count is: " + i);
}

}
};

Runnable run1 = new Runnable() {

@Override
public void run() {
int i = 0;
while (i < 1000) {
table.put("key" + i, "" + i);
i++;
}
}


};
new Thread(run1).start();
new Thread(run2).start();
}

}
posted on 2011-03-16 14:12  Android火鸟  阅读(509)  评论(0编辑  收藏  举报