java util concurrent包 3 -线程间通信wait/notify和await/signal

首先给大家分享一个巨牛巨牛的人工智能教程,是我无意中发现的。教程不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵段子,像看小说一样,哈哈~我正在学习中,觉得太牛了,所以分享给大家!点这里可以跳转到教程

                1 wait和notify

当调用wait()方法时线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备

Object的方法:void notify(): 唤醒一个正在等待该对象的线程。void notifyAll(): 唤醒所有正在等待该对象的线程。notifyAll使所有原来在该对象上等待被notify的线程统统退出wait状态,变成等待该对象上的锁,一旦该对象被解锁,它们会去竞争。notify只是选择一个wait状态线程进行通知,并使它获得该对象上的锁,但不惊动其它同样在等待被该对象notify的线程们,当第一个线程运行完毕以后释放对象上的锁,此时如果该对象没有再次使用notify语句,即便该对象已经空闲,其他wait状态等待的线程由于没有得到该对象的通知,继续处在wait状态,直到这个对象发出一个notify或notifyAll,它们等待的是被notify或notifyAll,而不是锁



2 wait和notify实例 子线程循环10次,主线程循环100次。接着子线程循环10次,主线程循环100次。如此循环50次。摘自张孝祥老师线程视频源码。
public class TraditionalThreadCommunication{ public static void main(String[] args) final Business business = new Business();  new Thread(new Runnable() {   public void run()   {    for (int i = 1; i <= 50; i++)    {     business.sub(i);    }   }  }).start();  for (int i = 1; i <= 50; i++)  {   business.main(i);  } }}class Business{ private boolean bShouldSub= true; public synchronized void sub(int i) while (!bShouldSub)  {   try   {    this.wait();   }   catch (InterruptedException e)   {    e.printStackTrace();   }  }  for (int j = 1; j <= 10; j++)  {   System.out.println("sub thread sequence of " + j + ",loop of " + i);  }  bShouldSub = falsethis.notify(); } public synchronizedvoid main(int i) while (bShouldSub)  {   try   {    this.wait();   }   catch (InterruptedException e)   {    e.printStackTrace();   }  }  for (int j = 1; j <= 100; j++)  {   System.out.println("main thread sequence of " + j + ",loop of " + i);  }  bShouldSub = truethis.notify(); }}


3 Condition实例

public class Condition1Test{ public static void main(String[] args) final BusinessLock business = new BusinessLock();  new Thread(new Runnable() {   public void run()   {    for (int i = 1; i <= 50; i++)    {     business.sub(i);    }   }  }).start();  for (int i = 1; i <= 50; i++)  {   business.main(i);  } }}class BusinessLock{ private boolean bShouldSub = true; ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); public void sub(int i) {  lock.lock();  try  {   while (!bShouldSub)   {    try    {     condition.await();    }    catch (InterruptedException e)    {     e.printStackTrace();    }   }   for (int j = 1; j <= 10; j++)   {    System.out.println("sub thread sequence of " + j + ",loop of " + i);   }   bShouldSub = false;   condition.signal();  }  finally  {   lock.unlock();  } } public void main(int i) {  lock.lock();  try  {   while (bShouldSub)   {    try    {     condition.await();    }    catch (InterruptedException e)    {     e.printStackTrace();    }   }   for (int j = 1; j <= 100; j++)   {    System.out.println("main thread sequence of " + j + ",loop of " + i);   }   bShouldSub = true;   condition.signal();  }  finally  {   lock.unlock();  } }}

4 Condition实例扩展
三个线程相互通信,main循环100次,sub2循环10次,sub3循环10次。接着main循环100次,sub2循环10次,sub3循环10次。如此循环50次。
public class Condition2Test{ public static void main(String[] args) final BusinessLock2 business = new BusinessLock2();  new Thread(new Runnable() {   public void run()   {    for (int i = 1; i <= 50; i++)    {     business.main(i);    }   }  }).start();  new Thread(new Runnable() {   public void run()   {    for (int i = 1; i <= 50; i++)    {     business.sub2(i);    }   }  }).start();  new Thread(new Runnable() {   public void run()   {    for (int i = 1; i <= 50; i++)    {     business.sub3(i);    }   }  }).start(); }}class BusinessLock2{ private int bShouldSub = 1; ReentrantLock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); public void main(int i) {  lock.lock();  try  {   while (bShouldSub != 1)   {    try    {     condition1.await();    }    catch (InterruptedException e)    {     e.printStackTrace();    }   }   for (int j = 1; j <= 100; j++)   {    System.out.println("main thread sequence of " + j + ",loop of " + i);   }   bShouldSub = 2;   condition2.signal();  }  finally  {   lock.unlock();  } } public void sub2(int i) {  lock.lock();  try  {   while (bShouldSub != 2)   {    try    {     condition2.await();    }    catch (InterruptedException e)    {     e.printStackTrace();    }   }   for (int j = 1; j <= 10; j++)   {    System.out.println("sub2 thread sequence of " + j + ",loop of " + i);   }   bShouldSub = 3;   condition3.signal();  }  finally  {   lock.unlock();  } } public void sub3(int i) {  lock.lock();  try  {   while (bShouldSub != 3)   {    try    {     condition3.await();    }    catch (InterruptedException e)    {     e.printStackTrace();    }   }   for (int j = 1; j <= 10; j++)   {    System.out.println("sub3 thread sequence of " + j + ",loop of " + i);   }   bShouldSub = 1;   condition1.signal();  }  finally  {   lock.unlock();  } }}

           

浏览人工智能教程

你可能感兴趣的:(java util concurrent包 3 -线程间通信wait/notify和await/signal)