国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java多線程通信:交替打印ABAB實例

瀏覽:3日期:2022-08-26 09:01:43

使用wait()和notify()實現(xiàn)Java多線程通信:兩個線程交替打印A和B,如ABABAB

public class Test { public static void main(String[] args) { final PrintAB print = new PrintAB(); new Thread(new Runnable() { public void run(){for(int i=0;i<5;i++) { print.printA(); } } }).start(); new Thread(new Runnable() { public void run() {for(int i=0;i<5;i++) { print.printB(); } } }).start(); } } class PrintAB{ private boolean flag = true; public synchronized void printA () { while(!flag) {try { this.wait();} catch (InterruptedException e) { e.printStackTrace(); } }System.out.print('A'); flag = false; this.notify(); } public synchronized void printB () { while(flag) {try { this.wait();} catch (InterruptedException e) { e.printStackTrace();} } System.out.print('B'); flag = true; this.notify(); } }

補充知識:Java多個線程順序打印數(shù)字

要求

啟動N個線程, 這N個線程要不間斷按順序打印數(shù)字1-N. 將問題簡化為3個線程無限循環(huán)打印1到3

方法一: 使用synchronized

三個線程無序競爭同步鎖, 如果遇上的是自己的數(shù)字, 就打印. 這種方式會浪費大量的循環(huán)

public class TestSequential1 { private volatile int pos = 1; private volatile int count = 0; public void one(int i) { synchronized (this) { if (pos == i) {System.out.println('T-' + i + ' ' + count);pos = i % 3 + 1;count = 0; } else {count++; } } } public static void main(String[] args) { TestSequential1 demo = new TestSequential1(); for (int i = 1; i <=3; i++) { int j = i; new Thread(()->{while(true) { demo.one(j);} }).start(); } }}

輸出

T-1 0T-2 5793T-3 5285T-1 2616T-2 33T-3 28T-1 22T-2 44T-3 6T-1 881T-2 118358T-3 247380T-1 30803T-2 29627T-3 52044...

方法二: 使用synchronized配合wait()和notifyAll()

競爭同步鎖時使用wait()和notifyAll(), 可以避免浪費循環(huán)

public class TestSequential4 { private volatile int pos = 1; private volatile int count = 0; private final Object obj = new Object(); public void one(int i) { System.out.println(i + ' try'); synchronized (obj) { System.out.println(i + ' in'); try {while (pos != i) { count++; System.out.println(i + ' wait'); obj.wait();}System.out.println('T-' + i + ' ' + count);pos = i % 3 + 1;count = 0;obj.notifyAll(); } catch (InterruptedException e) {e.printStackTrace(); } } } public static void main(String[] args) { TestSequential4 demo = new TestSequential4(); for (int i = 3; i >=1; i--) { int j = i; new Thread(()->{while(true) { demo.one(j);} }).start(); } }}

輸出

3 try3 in3 wait2 try2 in2 wait1 try1 inT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 2...

方法三: 使用可重入鎖

用Lock做, 非公平鎖, 三個線程競爭, 如果遇上的是自己的數(shù)字, 就打印. 這種方式也會浪費大量的循環(huán)

public class TestSequential2 { private final Lock lock = new ReentrantLock(); private volatile int pos = 1; private volatile int count = 0; public void one(int i) { lock.lock(); if (pos == i) { System.out.println('T-' + i + ' ' + count); pos = i % 3 + 1; count = 0; } else { count++; } lock.unlock(); } public static void main(String[] args) { TestSequential2 demo = new TestSequential2(); for (int i = 1; i <=3; i++) { int j = i; new Thread(()->{while(true) { demo.one(j);} }).start(); } }}

輸出

T-1 0T-2 0T-3 323T-1 54T-2 68964T-3 97642T-1 6504T-2 100603T-3 6989T-1 1313T-2 0T-3 183741T-1 233T-2 5081T-3 164367..

方法四: 使用可重入鎖, 啟用公平鎖

和3一樣, 但是使用公平鎖, 這種情況下基本上可以做到順序執(zhí)行, 偶爾會產(chǎn)生多一次循環(huán)

private final Lock lock = new ReentrantLock(true);

輸出

T-1 0T-2 0T-3 0T-1 0T-2 0T-3 0T-1 0T-2 0T-3 0T-1 0T-2 0T-3 1T-1 1T-2 1T-3 1...

方法五: 使用Condition

每個線程如果看到不是自己的計數(shù), 就await(), 如果是自己的計數(shù), 就完成打印動作, 再signalAll()所有其他線程去繼續(xù)運行, 自己在下一個循環(huán)后, 即使又繼續(xù)執(zhí)行, 也會因為計數(shù)已經(jīng)變了而await.

如果ReentrantLock構(gòu)造參數(shù)使用true, 可以基本消除 ~await 這一步的輸出.

public class ReentrantLockCondition2 { private static Lock lock = new ReentrantLock(); private static Condition condition = lock.newCondition(); private volatile int state = 1; private void handle(int state) { lock.lock(); try { while(true) {while(this.state != state) { System.out.println(state + ' ~await'); condition.await();}System.out.println(state);this.state = state % 3 + 1;condition.signalAll();System.out.println(state + ' await');condition.await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { ReentrantLockCondition2 rlc = new ReentrantLockCondition2(); new Thread(()->rlc.handle(1)).start(); new Thread(()->rlc.handle(2)).start(); new Thread(()->rlc.handle(3)).start(); }}

方法六: 使用多個Condition

給每個線程不同的condition. 這個和4的區(qū)別是, 可以用condition.signal()精確地通知對應(yīng)的線程繼續(xù)執(zhí)行(在對應(yīng)的condition上await的線程, 可能是多個). 這種情況下是可以多個線程都不unlock鎖的情況下進行協(xié)作的. 注意下面的while(true)循環(huán)是在lock.lock()方法內(nèi)部的.

public class ReentrantLockCondition { private static Lock lock = new ReentrantLock(); private static Condition[] conditions = {lock.newCondition(), lock.newCondition(), lock.newCondition()}; private volatile int state = 1; private void handle(int state) { lock.lock(); try { while(true) {while(this.state != state) { conditions[state - 1].await();}System.out.println(state);this.state = state % 3 + 1;conditions[this.state - 1].signal();conditions[state - 1].await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { ReentrantLockCondition rlc = new ReentrantLockCondition(); new Thread(()->rlc.handle(1)).start(); new Thread(()->rlc.handle(2)).start(); new Thread(()->rlc.handle(3)).start(); }}

以上這篇Java多線程通信:交替打印ABAB實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 眉山市| 昌都县| 名山县| 长治县| 滨州市| 呼图壁县| 城口县| 浮山县| 三亚市| 错那县| 孝义市| 邵阳县| 怀柔区| 北宁市| 广灵县| 玉山县| 宁化县| 阳东县| 阿坝| 吴川市| 革吉县| 石河子市| 申扎县| 阿尔山市| 清徐县| 湾仔区| 犍为县| 海城市| 洛阳市| 武鸣县| 财经| 营口市| 阜宁县| 南靖县| 永兴县| 高青县| 娄烦县| 荥阳市| 承德市| 内黄县| 萨迦县|