Java 多线程

 多线程模拟并发

    @Test
    public void test1() throws InterruptedException {
        final CountDownLatch latch= new CountDownLatch(4);//使用java并发库concurrent
        //启用10个线程
        for(int i=1;i<=10;i++){
            new Thread(new Runnable(){
                public void run(){
                    try {
                        //Thread.sleep(100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(String.format("子线程%s执行!",Thread.currentThread().getName()));
                    latch.countDown();//让latch中的数值减一
                }
            }).start();
        }
        //主线程
        latch.await();//阻塞当前线程直到latch中数值为零才执行
        System.out.println("主线程执行!");
    }

 

 

 

 http://www.cnblogs.com/zuoxiaolong/p/con2.html

 

http://www.cnblogs.com/zuoxiaolong/p/life27.html

https://www.cnblogs.com/lsy131479/p/8698706.html

https://www.cnblogs.com/NeilZhang/p/8781897.html

https://www.cnblogs.com/Java3y/p/8862953.html

http://www.cnblogs.com/stateis0/category/1206892.html

https://blog.csdn.net/column/details/vicky-juc.html

posted @ 2018-04-03 08:17  ~沐风  阅读(162)  评论(0编辑  收藏  举报