高手帮忙,我用线程池处理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);
            }
        }
}
}

解决方案 »

  1.   

    你的thread一个也没start,run方法会执行么?
      

  2.   

    个人觉得,count的问题,把count++也放在synchronized里面应该可以,
    没测试。
      

  3.   

    class myThread extends Thread {
    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);
    }
    }
    }
    }