DEMO01:
/** * 使用Object wait notify 控制线程通信(第一种方式) * * 并实现生产--》提货--》再次生产---》再次提货的循环体系,加载sleep时间,类似定时器, * 但是有区别,本实例在不同的线程执行,有生产线程,有消费线程 */
public class ObjectWaitNotify { static Object object=new Object(); public static List<String> list=new ArrayList(); public static void main(String[] args) { new Thread(() -> { while (true){ synchronized (object){ try { System.out.println("等待供货商通知取货----"); object.wait(); System.out.println("收到通知,可以提货"); if(list.size()>=4){ list.stream().forEach(s -> System.out.println("取出所有的包子--"+s)); } System.out.println("已提取所有的货物---"); System.out.println("清空仓库----"); list.clear(); object.notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程B").start(); new Thread(() -> { synchronized (object){ System.out.println("当前正在备货中-----"); for (int i = 0; i < 4; i++) { list.add("包子"+i); } System.out.println("通知代理商取货---"); object.notifyAll(); try { object.wait(); Thread.sleep(100); while (true){ Thread.sleep(2000); System.out.println("等待代理商取货,取货后再次生产---"); if(list.size()==0){ for (int i = 0; i < 4; i++) { list.add("包子"+i); } System.out.println("通知代理商取货---"); object.notifyAll(); object.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } } },"线程A").start(); } }
DEMO02
/** * 线程间通信第二种方式,使用lock 和Condition方式 */
public class LockCondition { private static Lock lock=new ReentrantLock(); private static Condition condition = lock.newCondition(); public static List<String> list=new ArrayList(); public static void main(String[] args) { new Thread(() -> { while (true){ lock.lock(); try { System.out.println("等待供货商通知取货----"); condition.await(); System.out.println("收到通知,可以提货"); if(list.size()>=4){ list.stream().forEach(s -> System.out.println("取出所有的包子--"+s)); } System.out.println("已提取所有的货物---"); break; } catch (InterruptedException e) { e.printStackTrace(); } lock.unlock(); } },"线程B---消费线程").start(); new Thread(() -> { lock.lock(); System.out.println("当前正在备货中-----"); for (int i = 0; i < 4; i++) { list.add("包子"+i); } System.out.println("通知代理商取货---"); condition.signal(); lock.unlock(); },"线程A--生产线程").start(); } }
DEMO03
/** * 线程通讯的第三种方式 ---这是最简单的方式 */
public class VolatileCommunicate { private static volatile Boolean flag=false; public static List<String> list=new ArrayList(); public static void main(String[] args) { new Thread(() -> { System.out.println("等待供货商通知取货----"); while (true){ if(flag){ System.out.println("收到通知,可以提货"); if(list.size()>=4){ list.stream().forEach(s -> System.out.println("取出所有的包子--"+s)); } System.out.println("已提取所有的货物---"); break; } } },"线程B---消费线程").start(); new Thread(() -> { System.out.println("当前正在备货中-----"); for (int i = 0; i < 4; i++) { list.add("包子"+i); } System.out.println("通知代理商取货---"); flag=true; },"线程A--生产线程").start(); } }
DEMO04
/** * CountDownLatch 实现线程之间的通讯 */
public class CountDownLatchCommunicate { private static CountDownLatch countDownLatch = new CountDownLatch(4); public static List<String> list=new ArrayList(); public static void main(String[] args) { new Thread(() -> { System.out.println("1号消费者-等待供货商通知取货----"); while (true){ try { countDownLatch.await(); System.out.println("1号消费者-收到通知,可以提货"); if(list.size()>=4){ synchronized (list){ Iterator<String> iterator = list.iterator(); boolean conusmer=false; while (iterator.hasNext()){ System.out.println("1号消费者-取出所有的包子--"+iterator.next()); iterator.remove(); System.out.println("1号消费者提取后》》当前存货是="+list.size()); if(!conusmer){ conusmer=true; } } if(conusmer){ System.out.println("1号消费者-已提取所有的货物---"); }else { System.out.println("货物已被其他2号消费者抢光了"); } } } break; } catch (InterruptedException e) { e.printStackTrace(); } } },"线程B---消费线程1").start(); new Thread(() -> { System.out.println("2号消费者-等待供货商通知取货----"); while (true){ try { countDownLatch.await(); System.out.println("2号消费者-收到通知,可以提货"); if(list.size()>=4){ synchronized (list){ Iterator<String> iterator = list.iterator(); boolean conusmer=false; while (iterator.hasNext()){ System.out.println("2号消费者-取出所有的包子--"+iterator.next()); iterator.remove(); System.out.println("2号消费者提取后》》当前存货是="+list.size()); if(!conusmer){ conusmer=true; } } if(conusmer){ System.out.println("2号消费者-已提取所有的货物---"); }else { System.out.println("货物已被其他1号消费者抢光了"); } } } break; } catch (InterruptedException e) { e.printStackTrace(); } } },"线程B---消费线程2").start(); new Thread(() -> { System.out.println("当前正在备货中-----"); for (int i = 0; i < 4; i++) { list.add("包子"+i); countDownLatch.countDown(); } System.out.println("通知代理商取货---"); },"线程A--生产线程").start(); } }
DEMO05
/** * 使用 LockSupport 实现线程间通信 */
public class LockSupportCommunicat { public static List<String> list=new ArrayList(); public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("等待供货商通知取货----"); while (true) { LockSupport.park(); System.out.println("收到通知,可以提货"); if (list.size() >= 4) { list.stream().forEach(s -> System.out.println("取出所有的包子--" + s)); } System.out.println("已提取所有的货物---"); break; } }, "线程B---消费线程"); thread.start(); new Thread(() -> { System.out.println("当前正在备货中-----"); for (int i = 0; i < 4; i++) { list.add("包子"+i); } System.out.println("通知代理商取货---"); LockSupport.unpark(thread); },"线程A--生产线程").start(); } }
转载请注明出处,全部原创,验证可行才写的博客。