public class ThreadCommunicationExample { private static volatile boolean FLAG = false; private static Object object = new Object(); private static List list = new ArrayList(); private static CountDownLatch countDownLatch = new CountDownLatch(1); private static ReentrantLock lock = new ReentrantLock(); private static Condition condition = lock.newCondition(); public static void main(String[] args) { // byVolatile(); // byWaitNotify(); // byCountDownLatch(); // byReentrantLockAndCondition(); // byLockSupport(); } private static void byLockSupport() { /**基于 LockSupport 实现线程间的阻塞和唤醒 LockSupport 是一种非常灵活的实现线程间阻塞和唤醒的工具,使用它不用关注是等待线程先进行还是唤醒线程先运行,但是得知道线程的名字 */ Thread threadA = new Thread(() -> { lock.lock(); try { for (int i = 0; i < 10; i++) { if (i == 5) { System.out.println("ThreadA 调用 LockSupport.park()开始阻塞当前线程线程"); LockSupport.park(); } list.add(i); System.out.println("ThreadA---> list.size() = " + list.size()); } } finally { lock.unlock(); } }); Thread threadB = new Thread(() -> { if (list.size() == 5) { System.out.println("ThreadB 调用 LockSupport.unpark(threadA)开始释放线程threadA"); LockSupport.unpark(threadA); } if (list.size() != 1) { System.out.println("ThreadB---> list.size() = " + list.size()); System.out.println("ThreadB 调用 LockSupport.park()开始阻塞当前线程线程"); LockSupport.park(); } try { Thread.currentThread().sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程B 处于业务处理结束......list.size() = " + list.size()); }); Thread threadC = new Thread(() -> { if (list.size() != 1) { System.out.println("ThreadC 调用 LockSupport.unpark(threadB)开始释放线程threadB"); LockSupport.unpark(threadB); try { Thread.currentThread().sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程C 处于业务处理结束......list.size() = " + list.size()); } }); threadA.start(); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } threadB.start(); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } threadC.start(); } private static void byReentrantLockAndCondition() { /**使用 ReentrantLock 结合 Condition * 这种方式使用起来并不是很好,代码编写复杂,而且线程 B 在被 A 唤醒之后由于没有获取锁还是不能立即执行, * 也就是说,A 在唤醒操作之后,并不释放锁。这种方法跟 Object 的 wait()/notify() 一样 */ Thread threadA = new Thread(() -> { lock.lock(); try { for (int i = 0; i < 10; i++) { if (i == 5) { // System.out.println("ThreadA 调用 singal ,唤醒condition等待队列中随机一个线程"); // condition.signal(); System.out.println("ThreadA 调用 signalAll ,唤醒condition等待队列中所有线程"); condition.signalAll(); try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } list.add(i); System.out.println("ThreadA---> list.size() = " + list.size()); } } finally { lock.unlock(); } }); Thread threadB = new Thread(() -> { lock.lock(); try { if (list.size() != 5) { try { System.out.println("线程B 处于开始等待......"); condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程B 处于开始处理业务......list.size() = " + list.size()); // System.out.println("ThreadB 调用 singal ,唤醒condition等待队列中随机一个线程"); // condition.signal(); try { Thread.currentThread().sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程B 处于业务处理结束......list.size() = " + list.size()); } } finally { lock.unlock(); } }); Thread threadC = new Thread(() -> { lock.lock(); try { if (list.size() != 5) { try { System.out.println("线程C 处于开始等待......"); condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程C 处于开始处理业务......list.size() = " + list.size()); // System.out.println("ThreadC 调用 singal ,唤醒condition等待队列中随机一个线程"); // condition.signal(); try { Thread.currentThread().sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程C 处于业务处理结束......list.size() = " + list.size()); } } finally { lock.unlock(); } }); threadB.start(); threadC.start(); try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } threadA.start(); } private static void byCountDownLatch() { /** jdk1.5 之后在java.util.concurrent包下提供了很多并发编程相关的工具类,简化了并发编程代码的书写,CountDownLatch 基于 AQS 框架, 相当于也是维护了一个线程间共享变量 state。 */ Thread threadA = new Thread(() -> { for (int i = 0; i < 10; i++) { if (i == 5) { System.out.println(Thread.currentThread().getName() + " i:" + i + "线程A执行休眠1秒"); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } countDownLatch.countDown(); System.out.println(Thread.currentThread().getName() + " i:" + i + "线程A执行countDown"); } else { System.out.println(Thread.currentThread().getName() + " i:" + i); } } }); Thread threadB = new Thread(() -> { for (int i = 0; i < 10; i++) { if (i == 5) { try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " i:" + i + "线程B执行await"); } else { System.out.println(Thread.currentThread().getName() + " i:" + i); } } }); threadB.start(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } threadA.start(); } private static void byWaitNotify() { /**使用 Object 类的 wait()/notify() Object 类提供了线程间通信的方法:wait()、notify()、notifyAll(),它们是多线程通信的基础,而这种实现方式的思想自然是线程间通信。 注意:wait/notify 必须配合 synchronized 使用,wait 方法释放锁,notify 方法不释放锁。wait 是指在一个已经进入了同步锁的线程内, 让自己暂时让出同步锁,以便其他正在等待此锁的线程可以得到同步锁并运行,只有其他线程调用了notify(),notify并不释放锁, 只是告诉调用过wait()的线程可以去参与获得锁的竞争了,但不是马上得到锁,因为锁还在别人手里,别人还没释放, 调用 wait() 的一个或多个线程就会解除 wait 状态,重新参与竞争对象锁,程序如果可以再次得到锁,就可以继续向下运行。 */ /**notify()和notifyAll()都是用来用来唤醒调用wait()方法进入等待锁资源队列的线程,区别在于: notify() 唤醒正在等待此对象监视器的单个线程。 如果有多个线程在等待,则选择其中一个随机唤醒(由调度器决定),唤醒的线程享有公平竞争资源的权利 notifyAll() 唤醒正在等待此对象监视器的所有线程,唤醒的所有线程公平竞争资源 */ //由输出结果,在线程 A 发出 notify() 唤醒通知之后,依然是走完了自己线程的业务之后,线程 B 才开始执行,正好说明 notify() 不释放锁,而 wait() 释放锁。 Thread threadA = new Thread(() -> { synchronized (object) { for (int i = 0; i < 10; i++) { if (i != 5) { list.add(i); System.out.println(Thread.currentThread().getName() + ": list.size = " + list.size()); } else { // object.notify(); // System.out.println(Thread.currentThread().getName() + "调用notify随机唤醒一个阻塞线程"); object.notifyAll(); System.out.println(Thread.currentThread().getName() + "调用notify唤醒所有阻塞线程"); } } } }); Thread threadB = new Thread(() -> { synchronized (object) { while (true) { if (list.size() == 5) { System.out.println(Thread.currentThread().getName() + ": list.size = " + list.size() + "B线程开始处理业务......"); } else { try { System.out.println(Thread.currentThread().getName() + " 准备阻塞当前线程B"); object.wait(); System.out.println(Thread.currentThread().getName() + " 阻塞当前线程B 结束"); System.out.println(Thread.currentThread().getName() + ": list.size = " + list.size() + " B线程开始处理业务......"); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); Thread threadC = new Thread(() -> { synchronized (object) { while (true) { if (list.size() == 5) { System.out.println(Thread.currentThread().getName() + ": list.size = " + list.size() + "C线程开始处理业务......"); } else { try { System.out.println(Thread.currentThread().getName() + " 准备阻塞当前线程C"); object.wait(); System.out.println(Thread.currentThread().getName() + " 阻塞当前线程C 结束"); System.out.println(Thread.currentThread().getName() + ": list.size = " + list.size() + " C线程开始处理业务......"); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); //先让B线程拿到锁,业务逻辑中wait阻塞等待住,然后释放锁; 这个时候A线程获取到锁,业务逻辑中触发B线程的唤醒,但是B需要去获取锁,由于 //A线程调用notify是不会释放锁的,所以只有A的加锁代码运行结束才会释放锁。这个时候只有B竞争到锁了,才会触发wait后面的业务 threadB.start(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } threadC.start(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } threadA.start(); } private static void byVolatile() { /**基于 volatile 关键字来实现线程间相互通信是使用共享内存的思想。大致意思就是多个线程同时监听一个变量, 当这个变量发生变化的时候 ,线程能够感知并执行相应的业务。这也是最简单的一种实现方式.*/ Thread threadA = new Thread(() -> { Stream.iterate(0, n -> n + 1).limit(10).forEach(n -> { if (n == 8) { FLAG = true; System.out.println(Thread.currentThread().getName() + ": " + n + "---> FLAG = " + FLAG); return; } System.out.println(Thread.currentThread().getName() + ": " + n + "---> FLAG = " + FLAG); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); }); Thread threadB = new Thread(new Runnable() { @Override public void run() { while (!FLAG) { System.out.println("B线程等待中......"); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + ": #### B线程开始执行业务 ### FLAG = " + FLAG); } }); threadB.start(); threadA.start(); }
多线程之间的5中通信方式
最新推荐文章于 2022-07-19 21:36:45 发布