日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

三个不同线程顺序打印ABC十种写法,看到就是赚到!

發(fā)布時(shí)間:2024/9/27 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 三个不同线程顺序打印ABC十种写法,看到就是赚到! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

夫陶公清風(fēng)千古,余又何人,敢稱庶幾

個(gè)人博客地址:http://www.breez.work


📖寫法一:使用信號(hào)量Semaphore

public class Foo {private Semaphore semaphoreB = new Semaphore(0);private Semaphore semaphoreC = new Semaphore(0);public Foo() {}public void first() throws InterruptedException {System.out.println("A");semaphoreB.release();}public void second() throws InterruptedException {semaphoreB.acquire();System.out.println("B");semaphoreC.release();}public void third() throws InterruptedException {semaphoreC.acquire();System.out.println("C");} }

📖寫法二:加synchronized鎖

public class Foo {private Object lock = new Object();private boolean a;private boolean b;public Foo() {}public void first() throws InterruptedException {synchronized (lock) {System.out.println("A");a = true;lock.notifyAll();}}public void second() throws InterruptedException {synchronized (lock) {while (!a) {lock.wait();}System.out.println("B");b = true;lock.notifyAll();}}public void third() throws InterruptedException {synchronized (lock) {while (!b) {lock.wait();}System.out.println("C");}} }

📖寫法三:使用原子AtomicInteger

public class Foo {private AtomicInteger a2 = new AtomicInteger(0);private AtomicInteger a3 = new AtomicInteger(0);public Foo() {}public void first() throws InterruptedException {System.out.println("A");a2.incrementAndGet();}public void second() throws InterruptedException {while (a2.get() != 1) {}System.out.println("B");a3.incrementAndGet();}public void third() throws InterruptedException {while (a3.get() != 1) {}System.out.println("C");} }

📖寫法四:使用隊(duì)列BlockingQueue

public class Foo {BlockingQueue<String> queue2 = new ArrayBlockingQueue<>(1);BlockingQueue<String> queue3 = new ArrayBlockingQueue<>(1);public Foo() {}public void first() throws InterruptedException {System.out.println("A");queue2.offer("b");}public void second() throws InterruptedException {while (queue2.size() == 0) {}System.out.println("B");queue3.offer("c");}public void third() throws InterruptedException {while (queue3.size() == 0) {}System.out.println("C");} }

📖寫法五:使用Condition

public class Foo {private int number = 1;private Lock lock = new ReentrantLock();private Condition a = lock.newCondition();private Condition b = lock.newCondition();private Condition c = lock.newCondition();public Foo() {}public void first() throws InterruptedException {lock.lock();try {while (number != 1) {a.await();}System.out.println("A");number = 2;b.signal();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void second() throws InterruptedException {lock.lock();try {while (number != 2) {b.await();}System.out.println("B");number = 3;c.signal();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void third() throws InterruptedException {lock.lock();try {while (number != 3) {c.await();}System.out.println("C");} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}} }

📖寫法六:使用計(jì)數(shù)器CountDownLatch

public class Foo {private CountDownLatch c2 = new CountDownLatch(1);private CountDownLatch c3 = new CountDownLatch(1);public Foo() {}public void first() throws InterruptedException {System.out.println("A");c2.countDown();}public void second() throws InterruptedException {c2.await();System.out.println("B");c3.countDown();}public void third() throws InterruptedException {c3.await();System.out.println("C");}}

測(cè)試代碼

public static void main(String[] args) throws InterruptedException {Foo foo = new Foo();new Thread(() -> {try {foo.first();} catch (InterruptedException e) {e.printStackTrace();}}, "A").start();new Thread(() -> {try {foo.second();} catch (InterruptedException e) {e.printStackTrace();}}, "B").start();new Thread(() -> {try {foo.third();} catch (InterruptedException e) {e.printStackTrace();}}, "C").start();}

歡迎加入BreezAm技術(shù)交流群:3861 35311 【QQ群】

總結(jié)

以上是生活随笔為你收集整理的三个不同线程顺序打印ABC十种写法,看到就是赚到!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。