- public class JoinDemo {
- public void a(Thread joinThread) {
- System.out.println("a 线程执行...");
- joinThread.start();
- try {
- joinThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("a 线程执行完毕...");
- }
- public void b() {
- System.out.println("加塞线程执行...");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("加塞线程执行完毕...");
- }
- public static void main(String[] args) {
- JoinDemo joinDemo = new JoinDemo();
- Thread joinThread = new Thread(joinDemo::b);
- new Thread(() -> joinDemo.a(joinThread)).start();
- }
- }
- // 假设在主线程中执行了 t.join() 方法 (t 是一个线程对象)
- // 1. 需要弄明白一件事: 谁拿了这个锁? 主线程拿了这个锁
- // 2. 这个锁是谁的? 是 t 线程的 (t 是一个对象, t 对象的锁)
- // 3. 在这个方法中, 当前的对象是 t
- public final synchronized void join(long millis) throws InterruptedException {
- long base = System.currentTimeMillis(); // 获取当前的时间
- long now = 0;
- if (millis <0) {
- throw new IllegalArgumentException("timeout value is negative");
- }
- if (millis == 0) {
- while (isAlive()) { // 相当于 this.isAlive(), 它检测的是当前对象 t 线程是否处于活动状态
- wait(0); // 执行 wait(0), 这个 wait() 是让拿到锁的线程等待, 也就是主线程
- }
- } else {
- while (isAlive()) { // 同样检测 t 线程是否活动
- long delay = millis - now; // 延时多久
- if (delay <= 0) {
- break;
- }
- wait(delay); // 等待延时的秒数
- now = System.currentTimeMillis() - base;
- }
- }
- }
- import java.util.concurrent.CountDownLatch;
- public class CountDownLatchDemo {
- public static void main(String[] args) throws InterruptedException {
- CountDownLatch countDownLatch = new CountDownLatch(10); // 设置一个长度为 10 的计数器
- for (int i = 0; i < 10; i++) {
- new Thread(() -> {
- try {
- Thread.sleep((long) Math.random() * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "执行完毕");
- countDownLatch.countDown(); // 每执行完一个线程 计数器减一
- }).start();
- }
- countDownLatch.await(); // 在这里阻塞等待其他线程执行完毕
- System.out.println("主线程执行完毕");
- }
- }
- CyclicBarrier
- import java.util.Random;
- import java.util.concurrent.CyclicBarrier;
- public class CyclicBarrierDemo {
- public static void main(String[] args) {
- // 第二个参数是一个 Runnable 接口, 用于所有线程到达后执行, 交给最后一个到达的线程执行
- CyclicBarrier cyclicBarrier = new CyclicBarrier(10, () -> System.out.println("好, 人到齐了, 现在开始开会"));
- for (int i = 0; i <10; i++) {
- new Thread(() -> {
- try {
- Thread.sleep(new Random().nextInt(4000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "到达了...");
- // 在这里执行 cyclicBarrier 的等待, 如果这里不能执行, 程序就会一直卡住
- try {
- cyclicBarrier.await();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }).start();
- }
- }
- }
- Semaphore
- import java.util.concurrent.Semaphore;
- public class SemaphoreDemo {
- public static void main(String[] args) {
- Semaphore semaphore = new Semaphore(10); // 设置同一时间只允许 10 个线程执行
- while (true) {
- new Thread(() -> {
- // 拿到许可
- try {
- semaphore.acquire();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // 执行代码
- System.out.println(Thread.currentThread() + "执行了...");
- // 释放许可
- semaphore.release();
- }).start();
- }
- }
- }
- Exchanger
- import java.util.concurrent.Exchanger;
- public class ExchangerDemo {
- public static void main(String[] args) {
- Exchanger<String> exchanger = new Exchanger<>();
- new Thread(() -> {
- System.out.println("a 开始抓取数据...");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- String res = "123456";
- // 将数据交换
- try {
- // 将自己的数据交换出去, 并且拿到另一个线程交换过来的数据
- String value = exchanger.exchange(res);
- System.out.println("a 比对结果:" + value.equals(res));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }).start();
- new Thread(() -> {
- System.out.println("b 开始抓取数据...");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- String res = "12345";
- // 将数据交换
- try {
- String value = exchanger.exchange(res);
- System.out.println("b 比对结果:" + value.equals(res));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }).start();
- }
- }
来源: http://www.bubuko.com/infodetail-3333388.html