文章目录:
Demo1(多线程通信之计数案例:synchronized实现方式)
Demo2(多线程通信之计数案例:Lock + Condition实现方式)
写在前面
关键字 synchronized 与 wait()/notify()这两个方法一起使用可以实现等待/通知模式。Lock 锁的 newContition()方法返回 Condition 对象,Condition 类也可以实现等待/通知模式。
使用Condition 类可以进行选择性通知. Condition 接口中比较常用的两个方法:await()会使当前线程等待,同时会释放锁,当其他线程调用 signal()时,线程会重新获得锁并继续执行,signal()用于唤醒一个等待的线程。
注意:在调用Condition的await()/signal()方法前,也需要线程持有相关的 Lock 锁。调用 await()后线程会释放这个锁,在 singal()调用后会从当前 Condition 对象的等待队列中,唤醒 一个线程,唤醒 的线程尝试获得锁, 一旦获得锁成功就继续执行。
也就是说,Condition接口中的await()方法就相当于之前与synchronized联合使用的Object类中的wait()方法;signal()方法就相当于之前与synchronized联合使用的Object类中的notify()方法。
Demo1(多线程通信之计数案例:synchronized实现方式)
这个案例主要实现让多个线程交替打印 0、1 这两个数字。incr 方法实现的增加1,当num不为0(此时为1)则进行wait等待,其余情况增加1,之后notifyAll唤醒其他等待的线程,这里使用while则是为了避免虚假唤醒的情况。
package test.count; /** * 多线程计数案例:synchronized实现方式 */ class Share { private int num = 0; public synchronized void incr() throws InterruptedException { while (num != 0) { this.wait(); } num++; System.out.println(Thread.currentThread().getName() + " :: " + num); this.notifyAll(); } public synchronized void decr() throws InterruptedException { while (num != 1) { this.wait(); } num--; System.out.println(Thread.currentThread().getName() + " :: " + num); this.notifyAll(); } } public class CountNum { public static void main(String[] args) { Share share = new Share(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { share.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程A").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { share.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程B").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { share.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程C").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { share.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程D").start(); } }
Demo2(多线程通信之计数案例:Lock + Condition实现方式)
与Demo1是一样的理解,只是这里实现方式变了。这里也是首先 lock.lock() 获取锁,incr 方法实现的增加1,当num不为0(此时为1)则进行await等待,其余情况增加1,之后signalAll唤醒其他等待的线程,这里使用while则是为了避免虚假唤醒的情况。最后为了确保锁一定能释放,将 lock.unlock() 写在finally子句中。
package test.count; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 多线程计数案例:Lock实现方式 */ class Share2 { private int num = 0; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void incr() { lock.lock(); try { while (num != 0) { condition.await(); } num++; System.out.println(Thread.currentThread().getName() + " :: " + num); condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void decr() { lock.lock(); try { while (num != 1) { condition.await(); } num--; System.out.println(Thread.currentThread().getName() + " :: " + num); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } public class CountNum2 { public static void main(String[] args) { Share2 share2 = new Share2(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { share2.incr(); } } },"线程A").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { share2.decr(); } } },"线程B").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { share2.incr(); } } },"线程C").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { share2.decr(); } } },"线程D").start(); } }
Demo3(多线程定制化通信之循环交替打印案例)
这个案例实现的是:三个线程(a、b、c)循环交替打印,也就是线程a打印5行、线程b接着打印10行、线程c继续打印15行、线程a再打印5行......,这样一直循环下去。
首先是定义了一个标志位 flag(线程a对应1、线程b对应2、线程c对应3),三个方法中依次获取锁,然后满足条件就打印、不满足就等待,之后每打印完一次就修改下标志位为下一个打印的线程标志位,之后将要打印的线程唤醒即可。
package test; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 线程间的定制化通信 */ class ShareResource { //定义标志位 //1 ---> a , 2 ---> b , 3 ---> c private int flag = 1; private Lock lock = new ReentrantLock(); private Condition a = lock.newCondition(); // a private Condition b = lock.newCondition(); // b private Condition c = lock.newCondition(); // c //打印5次,参数为打印的轮数 public void print5(int loop) throws InterruptedException { lock.lock(); try { while (flag != 1) { a.await(); } for (int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " :: " + i + " , 轮数: " + loop); } flag = 2; b.signal(); } finally { lock.unlock(); } } //打印10次,参数为打印的轮数 public void print10(int loop) throws InterruptedException { lock.lock(); try { while (flag != 2) { b.await(); } for (int i = 1; i <= 10; i++) { System.out.println(Thread.currentThread().getName() + " :: " + i + " , 轮数: " + loop); } flag = 3; c.signal(); } finally { lock.unlock(); } } //打印15次,参数为打印的轮数 public void print15(int loop) throws InterruptedException { lock.lock(); try { while (flag != 3) { c.await(); } for (int i = 1; i <= 15; i++) { System.out.println(Thread.currentThread().getName() + " :: " + i + " , 轮数: " + loop); } flag = 1; a.signal(); } finally { lock.unlock(); } } } public class ThreadDemo1 { public static void main(String[] args) { ShareResource shareResource = new ShareResource(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 3; i++) { try { shareResource.print5(i); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程a").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 3; i++) { try { shareResource.print10(i); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程b").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 3; i++) { try { shareResource.print15(i); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程c").start(); } }