java實(shí)現(xiàn)多線程交替打印兩個(gè)數(shù)
本文實(shí)例為大家分享了java實(shí)現(xiàn)多線程交替打印兩個(gè)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
方法1、使用wait和notify
package com.thread; public class T01 { public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); Object object = new Object(); Thread thread1 = new Thread(() -> { synchronized(object){//使用notify和wait時(shí),必須要選獲取到鎖 for (int i = 0; i < char1.length; i++) { try { System.out.print(char1[i]); object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } object.notify();//必須加上,否則程序無(wú)法結(jié)束,兩個(gè)線程總有一個(gè)最后是wait狀態(tài),所以此處必須加 } },"t1"); Thread thread2 = new Thread( () -> { synchronized(object){ for (int i = 0; i < char2.length; i++) { try { System.out.print(char2[i]); object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } object.notify(); } },"t2"); thread1.start(); thread2.start(); } }
方法2、使用LockSupport方法
package com.thread; import java.util.concurrent.locks.LockSupport; public class T02 { static Thread thread1 ; static Thread thread2 ; public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); thread1 = new Thread(() -> { for (int i = 0; i < char1.length; i++) { System.out.print(char1[i]); LockSupport.unpark(thread2); LockSupport.park(); } },"t1"); thread2 = new Thread(() -> { for (int i = 0; i < char2.length; i++) { LockSupport.park(); System.out.print(char2[i]); LockSupport.unpark(thread1); } },"t2"); thread1.start(); thread2.start(); } }
方法3、使用CAS自旋鎖
package com.thread; public class T03 { enum ReadEnum{ T1, T2; } static volatile ReadEnum r = ReadEnum.T1; public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); Thread thread1 = new Thread(() ->{ for (int i = 0; i < char1.length; i++) { while (r != ReadEnum.T1) { } System.out.print(char1[i]); r = ReadEnum.T2; } },"t1"); Thread thread2 = new Thread(() ->{ for (int i = 0; i < char2.length; i++) { while (r != ReadEnum.T2) { } System.out.print(char2[i]); r = ReadEnum.T1; } },"t2"); thread1.start(); thread2.start(); } }
方法4、使用Condition方法
package com.thread; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class T04 { public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); ReentrantLock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Thread thread1 = new Thread(() ->{ try { lock.lock(); for (int i = 0; i < char1.length; i++) { System.out.print(char1[i]); condition2.signal();//喚醒線程2執(zhí)行 condition1.await();//線程1等待 } condition2.signal(); }catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } },"t1"); Thread thread2 = new Thread(() ->{ try { lock.lock(); for (int i = 0; i < char2.length; i++) { System.out.print(char2[i]); condition1.signal(); condition2.await(); } condition1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } },"t2"); thread1.start(); thread2.start(); } }
Condition與notify相比的好處是,Condition可以指定需要喚醒的線程,而notify是無(wú)法指定的,只能隨機(jī)喚醒一個(gè)或者全喚醒(notifyAll)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用redis生成訂單號(hào)的實(shí)現(xiàn)示例
在電商系統(tǒng)中,生成唯一訂單號(hào)是常見(jiàn)需求,本文介紹如何利用SpringBoot和Redis實(shí)現(xiàn)訂單號(hào)的生成,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09基于String和List<String>間的相互轉(zhuǎn)換方式
這篇文章主要介紹了基于String和List間的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Spring Cloud項(xiàng)目前后端分離跨域的操作
這篇文章主要介紹了Spring Cloud項(xiàng)目前后端分離跨域的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java實(shí)現(xiàn)獲取行政區(qū)劃的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)獲取行政區(qū)劃的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)游戲2023-03-03java使用Jsoup連接網(wǎng)站超時(shí)的解決方法
jsoup是一個(gè)非常好的解析網(wǎng)頁(yè)的包,用java開(kāi)發(fā)的,提供了類(lèi)似DOM,CSS選擇器的方式來(lái)查找和提取文檔中的內(nèi)容,提取文檔內(nèi)容時(shí)會(huì)出現(xiàn)超時(shí)的情況,解決方法可看下文2013-11-11詳解如何使用ModelMapper庫(kù)進(jìn)行對(duì)象之間的屬性映射
這篇文章主要介紹了如何使用ModelMapper庫(kù)進(jìn)行對(duì)象之間的屬性映射實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會(huì)出現(xiàn)Spring Security的方法注解與AOP同時(shí)存在的問(wèn)題,這是就會(huì)設(shè)計(jì)順序問(wèn)題,需要的朋友可以參考下2023-10-10Java開(kāi)發(fā)崗位面試被問(wèn)到嵌套類(lèi)怎么辦
本篇文章主要介紹了深入理解Java嵌套類(lèi)和內(nèi)部類(lèi),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-07-07