java實(shí)現(xiàn)兩個(gè)線程交替打印的實(shí)例代碼
使用ReentrantLock實(shí)現(xiàn)兩個(gè)線程交替打印
實(shí)現(xiàn)字母在前數(shù)字在后
package com.study.pattern; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Demo2 { private static Lock lock = new ReentrantLock(); private static Condition c1 = lock.newCondition(); private static Condition c2 = lock.newCondition(); private static CountDownLatch count = new CountDownLatch(1); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Thread t1 = new Thread(() -> { try { lock.lock(); count.countDown(); for(char caa : ca) { c1.signal(); System.out.print(caa); c2.await(); } c1.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); Thread t2 = new Thread(() -> { try { count.await(); lock.lock(); for(char naa : na) { c2.signal(); System.out.print(naa); c1.await(); } c2.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); t1.start(); t2.start(); } }
最后輸出結(jié)果:
使用LinkedTransferQueue實(shí)現(xiàn)兩個(gè)線程交替打印
實(shí)現(xiàn)字母在前數(shù)字在后
package com.study.pattern; import java.util.concurrent.LinkedTransferQueue; public class Demo3 { private static LinkedTransferQueue<Character> linkedC = new LinkedTransferQueue<Character>(); private static LinkedTransferQueue<Character> linkedN = new LinkedTransferQueue<Character>(); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Thread t1 = new Thread(() -> { for(char caa : ca) { try { linkedC.put(caa); char a = linkedN.take(); System.out.print(a); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread t2 = new Thread(() -> { for(char naa : na) { try { char b = linkedC.take(); System.out.print(b); linkedN.put(naa); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); t2.start(); } }
輸出結(jié)果:
使用synchronized實(shí)現(xiàn)兩個(gè)線程交替打印
實(shí)現(xiàn)字母在前數(shù)字在后
package com.study.pattern; import java.util.concurrent.CountDownLatch; public class Demo4 { private static CountDownLatch count = new CountDownLatch(1); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Object lock = new Object(); Thread t1 = new Thread(() -> { synchronized (lock) { count.countDown(); for(char caa : ca) { System.out.print(caa); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); Thread t2 = new Thread(() -> { try { count.await(); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { for(char naa : na) { System.out.print(naa); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); t1.start(); t2.start(); } }
輸出結(jié)果:
使用LockSupport實(shí)現(xiàn)兩個(gè)線程交替打印
實(shí)現(xiàn)字母在前數(shù)字在后
package com.study.pattern; import java.util.concurrent.locks.LockSupport; public class Demo5 { private static Thread t1; private static Thread t2; public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); t1 = new Thread(() -> { for(char caa : ca) { System.out.print(caa); LockSupport.unpark(t2); LockSupport.park(); } }); t2 = new Thread(() -> { for(char naa : na) { LockSupport.park(); System.out.print(naa); LockSupport.unpark(t1); } }); t1.start(); t2.start(); } }
輸出結(jié)果:
以上就是java實(shí)現(xiàn)兩個(gè)線程交替打印的詳細(xì)內(nèi)容,感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
springboot實(shí)現(xiàn)異步任務(wù)
這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)異步任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05SpringBoot3結(jié)合Vue3實(shí)現(xiàn)用戶登錄功能
最近項(xiàng)目需求搭建一個(gè)結(jié)合Vue.js前端框架和Spring Boot后端框架的登錄系統(tǒng),本文主要介紹了SpringBoot3結(jié)合Vue3實(shí)現(xiàn)用戶登錄功能,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03使用EasyPoi完成復(fù)雜一對多excel表格導(dǎo)出功能全過程
這篇文章主要介紹了使用EasyPoi完成復(fù)雜一對多excel表格導(dǎo)出功能全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Spring-data-JPA使用時(shí)碰到的問題以及解決方案
這篇文章主要介紹了Spring-data-JPA使用時(shí)碰到的問題以及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié)
這篇文章主要介紹了關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Maven編譯遇到Process terminated問題(四種情況全部解決)
這篇文章主要介紹了Maven編譯遇到Process terminated問題(四種情況全部解決),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07mybatis查詢數(shù)據(jù)賦值到model里面為空的解決
這篇文章主要介紹了mybatis查詢數(shù)據(jù)賦值到model里面為空的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java基礎(chǔ)之?dāng)?shù)組的初始化過程
Java數(shù)組分為靜態(tài)和動(dòng)態(tài)初始化,靜態(tài)初始化中,程序員設(shè)定元素初始值,系統(tǒng)決定長度;動(dòng)態(tài)初始化中,程序員設(shè)定長度,系統(tǒng)提供初始值,數(shù)組初始化后長度固定,存儲(chǔ)在堆內(nèi)存中,數(shù)組變量在棧內(nèi)存指向堆內(nèi)存數(shù)組對象,基本類型數(shù)組存儲(chǔ)數(shù)據(jù)值,引用類型數(shù)組存儲(chǔ)對象引用2024-10-10