LockSupport
1. 简介为构建锁和同步器提供基本的线程阻塞唤醒原语,LockSupport中的park()和unpark()的作用分别是阻塞线程和解除阻塞线程。类似于wait和notify,类似于await和signal。 2. 使用示例// 1. pack unpackpublic static void main(String[] args){ Thread t1 = new Thre...
1. 简介为构建锁和同步器提供基本的线程阻塞唤醒原语,LockSupport中的park()和unpark()的作用分别是阻塞线程和解除阻塞线程。类似于wait和notify,类似于await和signal。 2. 使用示例// 1. pack unpackpublic static void main(String[] args){ Thread t1 = new Thre...
Condition1. 使用Condition实现等待通知机制public class ConTest { final Lock lock = new ReentrantLock(); final Condition condition = lock.newCondition(); public static void main(String[] args) ...
1. FeatureTaskFutureTask是Runnable, Future接口的实现类; { FutureTask<String> futureTask = new FutureTask<String>( () -> { System.out.println(Thread.currentThread().getNa...
如何中断一个线程?1. while循环判断interrupt标识{ Thread t1 = new Thread(() -> { while (true) { if(Thread.currentThread().isInterrupted()) { System.out.pr...
通过一些demo ,尝试理解juc包下的一些类的用法; 1. 使用ReentrantLock进行同步说明:开启四个线程,每个线程首先去执行lock方法后,睡眠不同的时间后,最后执行unlock方法。 @Testpublic void Test() { ReentrantLock reentrantLock = new ReentrantLock();//非公平锁 // A B...
CAS1. ABA问题{ static AtomicInteger atomicInteger = new AtomicInteger(100); static AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference<>(100,1); pu...
原子类1. LongAdder2. LongAccumulator{ LongAdder longAdder = new LongAdder(); longAdder.increment(); longAdder.increment(); longAdder.increment(); System.out.println(longAdder.sum()...
1. CountDownLatchlatch 的中文意思是门栓、栅栏,CountDownLatch 基于 AQS 的共享模式的使用 1.1 使用例子 假设我们有 N ( N > 0 ) 个任务,那么我们会用 N 来初始化一个 CountDownLatch,然后将这个 latch 的引用传递到各个线程中,在每个线程完成了任务后,调用 latch.countDown() 代表完成了一个任务...