package com.basic; /** * @program JavaBooks * @description: 通信 * @author: mf * @create: 2019/12/30 15:54 */ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * 实现一个容器,提供两个方法add size * 写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束 */ public class T11 { // List lists = new ArrayList(); // 解决方法1, 添加volatile,使t2能够得到通知 volatile List lists = new ArrayList(); public void add(Object o) { lists.add(o); } public int size() { return lists.size(); } public static void main(String[] args) { T11 t11 = new T11(); new Thread(() -> { for (int i = 0; i < 10; i++) { t11.add(new Object()); System.out.println("add " + i); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1").start(); new Thread(() -> { while (true) { if (t11.size() == 5) { break; } } System.out.println("t2结束"); }, "t2").start(); } } /** * 使用wait和notify优化 * wait释放锁,但是notify不释放 */ class T11_1 { List lists = new ArrayList(); public void add(Object o) { lists.add(o); } public int size() { return lists.size(); } public static void main(String[] args) { T11_1 t11_1 = new T11_1(); final Object lock = new Object(); new Thread(() -> { synchronized (lock) { System.out.println("t2 启动"); if (t11_1.size() != 5) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("t2 结束"); lock.notify(); } }, "t2").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> { System.out.println("t1 启动"); synchronized (lock) { for (int i = 0; i < 10; i++) { t11_1.add(new Object()); System.out.println("add " + i); if (t11_1.size() == 5) { lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }, "t1").start(); } } /** * 使用CountDownLatch CyclicBarrier semaphore */ class T11_2 { List lists = new ArrayList(); public void add (Object o) { lists.add(o); } public int size() { return lists.size(); } public static void main(String[] args) { T11_2 t11_2 = new T11_2(); CountDownLatch countDownLatch = new CountDownLatch(1); new Thread(() -> { System.out.println("t2启动"); if (t11_2.size() != 5) { try { countDownLatch.await(); // 上个门闩 } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("t2结束"); }, "t2").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> { System.out.println("t1启动"); for (int i = 0; i < 10; i++) { t11_2.add(new Object()); System.out.println("add " + i); if (t11_2.size() == 5) { // 打开门闩,让t2得以执行 countDownLatch.countDown(); } try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1").start(); } }