码迷,mamicode.com
首页 > 编程语言 > 详细

java线程池模拟并发

时间:2019-08-20 18:27:16      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:value   final   thread   ack   atomic   syn   countdown   pool   ace   

public class CountDownLatchTest1 implements Runnable{  
    final AtomicInteger number = new AtomicInteger();  
    volatile boolean bol = false;  
  
    @Override  
    public void run() {  
        System.out.println(number.getAndIncrement());  
        synchronized (this) {  
            try {  
                if (!bol) {  
                    System.out.println(bol);  
                    bol = true;  
                    Thread.sleep(10000);  
                }  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            System.out.println("并发数量为" + number.intValue());  
        }  
  
    }  
  
    public static void main(String[] args) {  
        ExecutorService pool = Executors. newCachedThreadPool();  
        CountDownLatchTest1 test = new CountDownLatchTest1();  
        for (int i=0;i<10;i++) {  
            pool.execute(test);  
        }  
    }  
}  

 

java线程池模拟并发

标签:value   final   thread   ack   atomic   syn   countdown   pool   ace   

原文地址:https://www.cnblogs.com/tinya/p/11384460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!