java 多线程 实现多个线程的顺序执行
场景
编写一个程序,启动三个线程,三个线程的name分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC...
使用 synchronized 实现
public class MyService { private int flag = 1; public synchronized void printA(){ while (flag != 1) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 2; this.notifyAll(); } public synchronized void printB(){ while (flag != 2) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 3; this.notifyAll(); } public synchronized void printC(){ while (flag != 3) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 1; this.notifyAll(); } }
这里的判断条件中用的是 while 而不是 if , 这两者之间有什么区别呢? 线程从 wait 状态被唤醒,并且获得锁以后会继续往下执行,比如 A 调用nofityAll() 唤醒 B,C,这时 B与C谁会先获得锁是不确定的。如果是C先获得了锁,那么C就继续往下执行打印,这与我们的期望的不符。所以这里我们使用了一个 while,当C获得锁以后再去判断一下flag,如果这时还不是它执行的时候,它就再次进入wait状态。此时A与C都是wait状态,获得锁的一定是B,从而实现我们期望的顺序打印。
测试类
package testABC; public class TestMain { public static void main(String[] args) { //编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC... // MyService service = new MyService(); MyService2 service = new MyService2(); Thread A = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { service.printA(); } } }); A.setName("A"); Thread B = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { service.printB(); } } }); B.setName("B"); Thread C = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { service.printC(); } } }); C.setName("C"); A.start(); B.start(); C.start(); } }
使用 Lock 实现
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MyService2 { private int flag = 1; private Lock lock = new ReentrantLock(); private Condition conditionA = lock.newCondition(); private Condition conditionB = lock.newCondition(); private Condition conditionC = lock.newCondition(); public void printA() { try { lock.lock(); if (flag != 1) { try { conditionA.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 2; conditionB.signal(); } finally { lock.unlock(); } } public void printB() { try { lock.lock(); if (flag != 2) { try { conditionB.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 3; conditionC.signal(); } finally { lock.unlock(); } } public void printC() { try { lock.lock(); if (flag != 3) { try { conditionC.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(Thread.currentThread().getName()); flag = 1; conditionA.signal(); } finally { lock.unlock(); } } }
当使用LOCK时可以不使用while因为condition可以唤醒指定的线程。同时注意必须先调用 conditionA.signal(); 再调用 lock.unlock(); ,否则会抛 java.lang.IllegalMonitorStateException 异常。因为在调用unlock之后,当前线程已不是此监视器对象condition的持有者。也就是说要在此线程持有锁定对象时,才能使用此锁定对象。
关于此异常的博文:关于java.lang.IllegalMonitorStateException