IT虾米网

Java模拟并发详解

admin 2018年05月30日 编程语言 143 0
public class Bingfa {

    public static void main(String[] args) throws InterruptedException {            
        // 锁住所有线程,等待并发执行  
        final CountDownLatch begin = new CountDownLatch(1);      
        final ExecutorService exec = Executors.newFixedThreadPool(10);    
  
        for (int index = 0; index < 100; index++)   
        {  
            final int NO = index + 1;                 
            Runnable run = new Runnable()   
            {  
                public void run() {    
                    try {    
                        // 等待,所有一起执行  
                        begin.await();  
                        //*****执行程序去********//
          ????????????
                      //*****执行程序去********//
                    } catch (InterruptedException e)   
                    {    
                        e.printStackTrace();  
                    }   
                    finally {    
                    }    
                }    
            };    
            exec.submit(run);  
        }    
          
        System.out.println("开始执行");    
        // begin减一,开始并发执行  
        begin.countDown();              
        //关闭执行  
        exec.shutdown();    
    }
}
public class CountdownLatchTest {  
  
    public static void main(String[] args) {  
        ExecutorService service = Executors.newCachedThreadPool(); //创建一个线程池  
        final CountDownLatch cdOrder = new CountDownLatch(1);//构造方法参数指定计数的次数  
        final CountDownLatch cdAnswer = new CountDownLatch(3);//构造方法参数指定计数的次数  
        for(int i=0;i<3;i++){  
            Runnable runnable = new Runnable(){  
                public void run(){  
                    try {  
                        System.out.println("线程" + Thread.currentThread().getName() +  
                                "正准备接受命令");  
                        cdOrder.await(); //战士们都处于等待命令状态  
                        System.out.println("线程" + Thread.currentThread().getName() +  
                                "已接受命令");  
                        Thread.sleep((long)(Math.random()*10000));  
                        System.out.println("线程" + Thread.currentThread().getName() +  
                                "回应命令处理结果");  
  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    } finally {  
                        cdAnswer.countDown(); //任务执行完毕,返回给指挥官,cdAnswer减1。  
                    }  
                }  
            };  
            service.execute(runnable);//为线程池添加任务  
        }  
        try {  
            Thread.sleep((long)(Math.random()*10000));  
  
            System.out.println("线程" + Thread.currentThread().getName() +  
                    "即将发布命令");  
            cdOrder.countDown(); //发送命令,cdOrder减1,处于等待的战士们停止等待转去执行任务。  
            System.out.println("线程" + Thread.currentThread().getName() +  
                    "已发送命令,正在等待结果");  
            cdAnswer.await(); //命令发送后指挥官处于等待状态,一旦cdAnswer为0时停止等待继续往下执行  
            System.out.println("线程" + Thread.currentThread().getName() +  
                    "已收到所有响应结果");  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {   
        }  
        service.shutdown(); //任务结束,停止线程池的所有线程  
    }  
}


发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

slenium截屏详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。