Java之多个线程顺序循环执行的几种实现
作者:_小马快跑_
这篇文章主要介绍了Java之多个线程顺序循环执行的几种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
ReentrantLock+Condition方式实现
public class ReentrantLock_Impl { private static final String FLAG_THREAD_1 = "ReentrantLock_Thread1"; private static final String FLAG_THREAD_2 = "ReentrantLock_Thread2"; private static final String FLAG_THREAD_3 = "ReentrantLock_Thread3"; public static void main(String[] args) throws InterruptedException { //ReentrantLock构造方法传入true为公平锁 false为非公平锁 ReentrantLock lock = new ReentrantLock(true); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); FairRunnable runnable = new FairRunnable(lock, condition1, condition2, condition3); new Thread(runnable, FLAG_THREAD_1).start(); new Thread(runnable, FLAG_THREAD_2).start(); new Thread(runnable, FLAG_THREAD_3).start(); } static class FairRunnable implements Runnable { private ReentrantLock lock; private Condition condition1; private Condition condition2; private Condition condition3; public FairRunnable(ReentrantLock lock, Condition condition1, Condition condition2, Condition condition3) { this.lock = lock; this.condition1 = condition1; this.condition2 = condition2; this.condition3 = condition3; } @Override public void run() { while (true) { try { lock.lock(); System.out.println(Thread.currentThread().getName() + "开始执行"); Thread.sleep(1000); switch (Thread.currentThread().getName()) { case FLAG_THREAD_1: //唤醒线程2 自身线程挂起阻塞 condition2.signal(); condition1.await(); break; case FLAG_THREAD_2: //唤醒线程3 自身线程挂起阻塞 condition3.signal(); condition2.await(); break; case FLAG_THREAD_3: //唤醒线程1 自身线程挂起阻塞 condition1.signal(); condition3.await(); break; } } catch (Exception e) { e.printStackTrace(); return; } finally { lock.unlock(); } } } } }
执行结果:
ReentrantLock_Thread1开始执行
ReentrantLock_Thread2开始执行
ReentrantLock_Thread3开始执行
ReentrantLock_Thread1开始执行
ReentrantLock_Thread2开始执行
ReentrantLock_Thread3开始执行
......
Synchronized+wait/notifyAll方式实现
public class Synchronized_Impl { private static final String FLAG_THREAD_1 = "Synchronized_Thread1"; private static final String FLAG_THREAD_2 = "Synchronized_Thread2"; private static final String FLAG_THREAD_3 = "Synchronized_Thread3"; public static void main(String[] args) { FairRunnable runnable = new FairRunnable(); new Thread(runnable, FLAG_THREAD_1).start(); new Thread(runnable, FLAG_THREAD_2).start(); new Thread(runnable, FLAG_THREAD_3).start(); } static class FairRunnable implements Runnable { private volatile static int flag = 1; private final Object object = new Object(); @Override public void run() { while (true) { synchronized (object) { //如果当前情况是:线程1&flag!=1、线程2&flag!=2、线程3&flag!=3 那当前线程通过object.wait挂起 switch (Thread.currentThread().getName()) { case FLAG_THREAD_1: while (flag != 1) { try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } break; case FLAG_THREAD_2: while (flag != 2) { try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } break; case FLAG_THREAD_3: while (flag != 3) { try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } break; } //线程任务开始执行 System.out.println(Thread.currentThread().getName() + "开始执行"); try { //模拟线程任务执行 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } switch (Thread.currentThread().getName()) { case FLAG_THREAD_1: //接下来该去执行线程2 flag = 2; break; case FLAG_THREAD_2: //接下来该去执行线程3 flag = 3; break; case FLAG_THREAD_3: //接下来该去执行线程1 flag = 1; break; } //唤醒所有线程 object.notifyAll(); } } } } }
执行结果:
Synchronized_Thread1开始执行
Synchronized_Thread2开始执行
Synchronized_Thread3开始执行
Synchronized_Thread1开始执行
Synchronized_Thread2开始执行
Synchronized_Thread3开始执行
......
两者对比
使用ReentrantLock+Condition可以更准确的控制唤醒哪一个线程;
而Synchronized+wait/notifyAll的方式可能会出现获取锁的线程并不是目标线程,此时获取锁的线程会重新挂起,直到获取锁的线程即是目标线程为止。
比如上面代码中:
线程1执行完后调用notifyAll(), 此时线程2、线程3都会被唤醒并尝试获取锁,如果此时线程3获得锁,那么线程2还要继续等待,线程3执行时发现并不是要执行的目标线程,那么线程3会调用wait()挂起,此时entrySet中只有线程2了,线程2也是要执行的目标线程,此时才会去执行线程2,整个过程多了一次线程3的获取与释放锁,原因就是wait/notifyAll并不能像ReentrantLock+Condition一样能精确地唤醒某个线程。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。