java線程池模擬並發


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);  
        }  
    }  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM