java线程池模拟并发
Posted tinya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
以上是关于java线程池模拟并发的主要内容,如果未能解决你的问题,请参考以下文章
Java 并发编程线程池机制 ( 线程池示例 | newCachedThreadPool | newFixedThreadPool | newSingleThreadExecutor )