高手帮忙,我用线程池处理MAP的PUT方法,插入十万条数据,但始终最后没有这么多。加了锁或者采用HASHTABLE都没用,代码如下,请帮忙解释,提出一些方法。
package TestMap;import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TestMap extends Thread{
public static ExecutorService pools = Executors.newFixedThreadPool(10);
static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
static int count=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMap tm = new TestMap();
for(int i=0;i<100000;i++){
pools.submit(tm.new myThread());
System.out.println("循环次数"+i);
}
}
class myThread extends Thread{
public void run(){
count++;
synchronized(map){
map.put(count, count);
}
System.out.println("Map个数"+map.size());
if(count==100000){
System.out.println("Map数:"+map.size()+" -- 插入数:"+count);
}
}
}
}
package TestMap;import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TestMap extends Thread{
public static ExecutorService pools = Executors.newFixedThreadPool(10);
static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
static int count=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMap tm = new TestMap();
for(int i=0;i<100000;i++){
pools.submit(tm.new myThread());
System.out.println("循环次数"+i);
}
}
class myThread extends Thread{
public void run(){
count++;
synchronized(map){
map.put(count, count);
}
System.out.println("Map个数"+map.size());
if(count==100000){
System.out.println("Map数:"+map.size()+" -- 插入数:"+count);
}
}
}
}
没测试。
public void run() {
synchronized (map) {
count++;
map.put(count, count);
System.out.println("Map个数" + map.size());
if (count == 100000) {
System.out.println("Map数:" + map.size() + " -- 插入数:" + count);
}
}
}
}