Java synchronized线程交替运行实现过程详解
作者:程序员吉尔
这篇文章主要介绍了Java synchronized线程交替运行实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
背景
用两个线程交替输出A-Z和1-26,即一个线程输出A-Z,另一个线程输出1-26
而且是交替形式
- 线程1输出A——线程二输出1
- 线程1输出B——线程二输出2
- 线程1输出C——线程二输出3
以此类推
分析
主要考察线程之间的通信,思路就是创建两个线程
在一个线程输出一个内容之后,自己进入阻塞,去唤醒另一个线程
另一个线程同样,输出一个内容之后,自己进入阻塞,去唤醒另一个线程
代码实现(一)
public class AlternateCover { public static void main(String[] args) { final char[] arrLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); final String[] arrNumber = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object();// 设置一个锁对象 // print arrNumber new Thread(() -> { synchronized (lock) { for (String a : arrNumber) { System.out.print( a); try { lock.notify();// 唤醒其他等待的线程 此处唤醒 arrLetter lock.wait();// arrNumber自己进入等待 让出CPU资源和锁资源 } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }, "arrNumber ").start(); // print arrLetter new Thread(() -> { synchronized (lock) {// 获取对象锁 for (char a : arrLetter) { System.out.print(a); try { lock.notify();// 唤醒其他等待的线程 此处唤醒 arrNumber lock.wait();// arrLetter自己进入等待 让出CPU资源和锁资源 } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify();// 最后那个等待的线程需要被唤醒,否则程序无法结束 } }, "arrLetter ").start(); } }
运行一下,确实实现了交替输出,但是多运行几次,就会发现问题
有时候是数字先输出,有时候是字母先输出
即两个线程谁先启动的顺序是不固定的
倘若试题中再加一句,必须要字母先输出,怎么办?
代码实现(二)
/** * 交替掩护 必须保证大写字母先输出 */ public class AlternateCover { public static volatile Boolean flg = false;// 谁先开始的标志 volatile修饰目的是让该值修改对所有线程可见,且防止指令重排序 public static void main(String[] args) { final char[] arrLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); final String[] arrNumber = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object();// 锁对象 // print arrLetter new Thread(() -> { synchronized (lock) { if (!flg){ // 如果flg是false 就将值设为true flg = true; } for (char a : arrLetter) { System.out.print(a);// 输出内容 try { lock.notify();// 唤醒在等待的其他线程中的一个(此处也只有另一个) lock.wait();// 自己进入等待 让出CPU资源和锁资源 } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify();// 最后那个等待的线程需要被唤醒,否则程序无法结束 } }, "arrLetter").start(); // print arrNumber new Thread(() -> { synchronized (lock) { if (!flg){// 倘若是该线程先执行,那么flg次数还是false 就先等着 try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (String a : arrNumber) { System.out.print( a); try { lock.notify(); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }, "arrNumber").start(); } }
如此问题可以得到解决,但有更优(装)雅(B)的解决办法
CountDownLatch实现
/** * 交替掩护 必须保证大写字母先输出 */ public class AlternateCover { private static CountDownLatch count = new CountDownLatch(1);// 计数器容量为1 public static void main(String[] args) { final char[] arrLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); final String[] arrNumber = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object(); // print arrLetter new Thread(() -> { synchronized (lock) {// 获取对象锁 count.countDown();// 对计数器进行递减1操作,当计数器递减至0时,当前线程会去唤醒阻塞队列里的所有线程(只针对count) for (char a : arrLetter) { System.out.print(a); try { lock.notify();// 唤醒其他等待的线程 此处唤醒 arrNumber lock.wait();// arrLetter自己进入等待 让出CPU资源和锁资源 } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify();// 最后那个等待的线程需要被唤醒,否则程序无法结束 } }, "arrLetter ").start(); // print arrNumber new Thread(() -> { synchronized (lock) { try { count.await();// 如果该线程先执行 阻塞当前线程,将当前线程加入阻塞队列 } catch (InterruptedException e) { e.printStackTrace(); } for (String a : arrNumber) { System.out.print( a); try { lock.notify();// 唤醒其他等待的线程 此处唤醒 arrLetter lock.wait();// arrNumber自己进入等待 让出CPU资源和锁资源 } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }, "arrNumber ").start(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。