package com.ietree.multithread.sync;import java.util.ArrayList;import java.util.List;public class MyList { private volatile static List list = new ArrayList(); public void add() { list.add("apple"); } public int size() { return list.size(); } public static void main(String[] args) { final MyList list1 = new MyList(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 10; i++) { list1.add(); System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素.."); Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { while (true) { if (list1.size() == 5) { System.out.println("当前线程收到通知:" + Thread.currentThread().getName() + " list size = 5 线程停止.."); throw new RuntimeException(); } } } }, "t2"); t1.start(); t2.start(); } }
程序输出:
当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. Exception in thread "t2" 当前线程收到通知:t2 list size = 5 线程停止.. java.lang.RuntimeException at com.ietree.multithread.sync.MyList$2.run(MyList.java:43) at java.lang.Thread.run(Unknown Source) 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素..
理解:线程Thread2不停地通过while语句检测这个条件(list.size()==5)是否成立 ,从而实现了线程间的通信。但是这种方式会浪费CPU资源。
使用这种方式实现线程通信需要注意:wait和notify必须配合synchronized关键字使用,wait方法释放锁,notify方法不释放锁。并且在这个例子中必须是Thread2先执行才可以。
List list = "apple" ListAdd2 list2 = Object lock = = Thread( ( i = 0; i < 10; i++"当前线程:" + Thread.currentThread().getName() + "添加了一个元素.."500 (list2.size() == 5"已经发出通知..""t1"= Thread( (list2.size() != 5"当前线程:" + Thread.currentThread().getName() + "收到通知线程停止.." "t2"
程序输出:
当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 已经发出通知.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t2收到通知线程停止.. Exception in thread "t2" java.lang.RuntimeException at com.ietree.multithread.sync.ListAdd3$2.run(ListAdd3.java:59) at java.lang.Thread.run(Unknown Source)
package com.ietree.multithread.sync;import java.util.ArrayList;import java.util.List;import java.util.concurrent.CountDownLatch;public class ListAdd2 { private volatile static List list = new ArrayList(); public void add() { list.add("apple"); } public int size() { return list.size(); } public static void main(String[] args) { final ListAdd2 list2 = new ListAdd2(); final CountDownLatch countDownLatch = new CountDownLatch(1); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 10; i++) { list2.add(); System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素.."); Thread.sleep(500); if (list2.size() == 5) { System.out.println("已经发出通知.."); countDownLatch.countDown(); } } // } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { if (list2.size() != 5) { try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("当前线程:" + Thread.currentThread().getName() + "收到通知线程停止.."); throw new RuntimeException(); } }, "t2"); t2.start(); t1.start(); } }
程序输出:
当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 已经发出通知.. Exception in thread "t2" 当前线程:t1添加了一个元素.. 当前线程:t2收到通知线程停止.. java.lang.RuntimeException at com.ietree.multithread.sync.ListAdd2$2.run(ListAdd2.java:56) at java.lang.Thread.run(Unknown Source) 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素.. 当前线程:t1添加了一个元素..
The above is the detailed content of Communication between threads in Java multithreading. For more information, please follow other related articles on the PHP Chinese website!