JAVA的异步线程与同步线程问题.

    public static void main(String[] args) {
        int threadCount = 1000; // 线程数量(1000)
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        List<Object> list = new ArrayList<Object>();
        
        MyThreadTest myThreadTest = new MyThreadTest(list);
       
        MySync mySync = MySync.getMySync(myThreadTest,countDownLatch);
        // mySync = new MySync(myThreadTest,countDownLatch);  // 为什么放开这段代码,会连 System.out.println("========:"+list.size()); 都不会执行了?
        
        try {
        for(int i =0;i<threadCount;i++){
            mySync.run();
        }
            countDownLatch.await(); // 主线程等待所有子线程执行完成,再向下执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("========:"+list.size());
    }
}

class MyThreadTest implements Runnable{
    private List<Object> list;
    
    private int i = 0;
    
    public MyThreadTest(List<Object> list){
        this.list = list;
    }
    
    @Override
    public void run() {
        i++;
        list.add(new Object());
        System.out.println(i);
    }
}

class MySync{

    private static CountDownLatch countDownLatch;
    private static Runnable runn;
    
    public MySync(){}
    
    @SuppressWarnings("static-access")
       public MySync(Runnable runn,CountDownLatch countDownLatch){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(MySync.class);
        enhancer.setCallback(new MethodInterceptorImpl());

        MySync mySync = (MySync)enhancer.create();
        mySync.countDownLatch = countDownLatch;
        mySync.runn = runn;
    }
    
    @SuppressWarnings("static-access")
    public static MySync getMySync(Runnable runn,CountDownLatch countDownLatch){
         Enhancer enhancer = new Enhancer();
         enhancer.setSuperclass(MySync.class);
         enhancer.setCallback(new MethodInterceptorImpl());
         
         MySync mySync = (MySync)enhancer.create();
         mySync.countDownLatch = countDownLatch;
         mySync.runn = runn;
         return mySync;
    }
    
    public void run() throws InterruptedException {
            MyThreadPool.execute(runn);
    }
    
    private static class MethodInterceptorImpl implements MethodInterceptor{

        @Override
        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            Object result = proxy.invokeSuper(obj, args);
            if("public void MySync.run() throws java.lang.InterruptedException".equals(method.toString())){
                countDownLatch.countDown(); // 完成一个子线程,减少一个计数
                return result;
            }
            return result;
        }
        
    }
}

class MyThreadPool {

    private static ExecutorService pool = null;
    
    @SuppressWarnings("unchecked")
    public static <T> T submit(Runnable runn){
        T result = null;
        if (pool == null) {
            pool = Executors.newCachedThreadPool();
        }
        try {
            Future<?> future = pool.submit(runn);
            if(future.isDone()==true){
                result = (T) future.get();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public static void execute(Runnable runn){
        if (pool == null) {
            pool = Executors.newCachedThreadPool();
        }
        try {
            pool.execute(runn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

System.out.println("========:"+list.size()); 使用计数来进行了同步,这段代码不应该是最后才会输出的吗?测试结果是不确定什么时候输出.
如果放开注释的那段代码,为什么连System.out.println("========:"+list.size()); 都不输出了?这是什么原因造成的?(不知道这段代码还有什么BUG,请各位大神指导)

查看回复