- 倒计时门闩
倒计时门闩会导致一条或多条线程在"门口"一直等待,直到另一条线程打开这扇门,线程才得以继续运行。它是由一个计数变量和两个操作数组成,这两个操作分别是"导致一条线程等待直到计数变为0"以及"递减计数变量"。
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
int count = 10;
CountDownLatch countDownLatch = new CountDownLatch(10);
ExecutorService executorService = Executors.newFixedThreadPool(count);
for (int i = 0; i < count; i++) {
executorService.execute(() -> {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName());
});
}
countDownLatch.await();
executorService.shutdown();
System.out.println("done");
}
}
- 同步屏障
同步屏障允许一组线程彼此相互等待,直到抵达某个公共的屏障点。因为该屏障在等待线程被释放之后可以重用,所以称它为可循环使用的屏障。该同步器对于固定数量并且互相之间必须不时等待彼此的多线程应用很有用。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CyclicBarrierDemo {
public static void main(String[] args) {
int count = 10;
CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> System.out.println("线程全部执行完成"));
ExecutorService executorService = Executors.newFixedThreadPool(count);
for (int i = 0; i < count; i++) {
executorService.execute(() -> {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(5000L);
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
- 交换器
交换器提供一个线程彼此之间能够交换对象的同步点。每条线程都会往这个交换器的exchange()方法传入一些对象,匹配伙伴线程,同时接受伙伴线程中的对象作为返回值。在诸如遗传算法和管道设计的应用程序中,交换器会很有用。
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExchangerDemo {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Exchanger<String> exchanger = new Exchanger<>();
executorService.execute(() -> {
try {
String result = exchanger.exchange("one");
System.out.println(Thread.currentThread().getName() + ": " + result);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
executorService.execute(() -> {
try {
String result = exchanger.exchange("two");
System.out.println(Thread.currentThread().getName() + ": " + result);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
executorService.shutdown();
}
}
- 信号量
信号量维护了一组许可证(permit),以约束访问被限制资源的线程数。当没有可用的许可证时,线程的获取尝试会一直阻塞,直到其他的线程释放一个许可证。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
public static void main(String[] args) {
int count = 5;
Semaphore semaphore = new Semaphore(count);
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
executorService.submit(() -> {
try {
semaphore.acquire();
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
semaphore.release();
});
}
executorService.shutdown();
}
}