多线程 07 java5原子性操作类和并发库的应用

java5线程并发库的应用

//ExecutorService threadPool = Executors.newFixedThreadPool(3);

//        ExecutorService threadPool = Executors.newCachedThreadPool();

        ExecutorService threadPool = Executors.newSingleThreadExecutor();

        for(int i=1;i<=10;i++){

            final  int task=i;

            threadPool.execute(new Runnable() {

                @Override

                public void run() {

                    for(int j=1;j<=10;j++){

扫描二维码关注公众号,回复: 4485894 查看本文章

                        try {

                            Thread.sleep(10);

                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                        System.out.println(Thread.currentThread().getName()+"is looping of "+j+"for task of "+task);

                    }

                }

            });

        }

System.out.println("task have committed");

        threadPool.shutdown();//执行完之后销毁线程

        threadPool.shutdownNow();//执行到这里之后立刻销毁线程

Executors.newScheduledThreadPool(3).schedule(new Runnable() {

            @Override

            public void run() {

                System.out.println("booming");

            }

        }, 1, TimeUnit.SECONDS);

如有疑问,请发邮件:[email protected]


github:  https://github.com/wangrui0/

猜你喜欢

转载自blog.csdn.net/qq_35524586/article/details/84972619