美文网首页
ExecutorService

ExecutorService

作者: jsjack_wang | 来源:发表于2017-12-15 16:31 被阅读0次
    public class PoolDemo {
        public static void main(String[] args) {
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
            for (int threadNumber = 0; threadNumber < 100; threadNumber ++) {
                createPushThread(fixedThreadPool);
            }
            try {
                fixedThreadPool.shutdown(); 
                fixedThreadPool.awaitTermination(1, TimeUnit.NANOSECONDS);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        
        private static void createPushThread(ExecutorService fixedThreadPool) {
            fixedThreadPool.execute(new Runnable() { 
                public void run() {
                    try {
                        Thread.sleep(2000);
                        System.out.println("do sth...");
                    } catch (Exception e) {
                    }
                }
            });
        }
    }

    相关文章

      网友评论

          本文标题:ExecutorService

          本文链接:https://www.haomeiwen.com/subject/nxcdwxtx.html