JUC 常用类

CountDownLatch
场景:用10个线程分治计算1到 n 之和
构造函数: CountDownLatch(int count)
方法: await/countDown

Semaphore
场景:实现一个有界的、可阻塞的、线程安全的 Set
构造函数: Semaphore(int permits)
方法: acquire/release

CyclicBarrier
场景:可重复使用的 CountDownLatch
构造函数: CyclicBarrier(int parties, Runnable barrierAction)
方法: await
说明: await 阻止当前线程运行下去,直到所有线程到达 await() 调用点。 barrierAction 是一个 Runnable 对象,通常用于在放行一组线程前做一些统计工作。这个类最不好掌握,详细参考 http://blog.csdn.net/shihuacai/article/details/8856407

FutureTask
场景: ExecutorService
构造函数: ExecutorService#submit(Callable<T> c)
方法: get()

ReentrantLock && Condition
场景:更精准的 wait/notifyAll
构造函数: ReentrantLock#newCondition()
方法: await/signalAll

猜你喜欢

转载自dsxwjhf.iteye.com/blog/2289368