您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家?guī)碛嘘Pleetcode中怎么利用多線程打印零與奇偶數,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
假設有這么一個類:
class ZeroEvenOdd {
public ZeroEvenOdd(int n) { ... } // 構造函數
public void zero(printNumber) { ... } // 僅打印出 0
public void even(printNumber) { ... } // 僅打印出 偶數
public void odd(printNumber) { ... } // 僅打印出 奇數
}
相同的一個 ZeroEvenOdd 類實例將會傳遞給三個不同的線程:線程 A 將調用 zero(),它只輸出 0 。
線程 B 將調用 even(),它只輸出偶數。
線程 C 將調用 odd(),它只輸出奇數。
每個線程都有一個 printNumber 方法來輸出一個整數。請修改給出的代碼以輸出整數序列 010203040506... ,其中序列的長度必須為 2n。
示例 1:
輸入:n = 2
輸出:"0102"
說明:三條線程異步執(zhí)行,其中一個調用 zero(),另一個線程調用 even(),最后一個線程調用odd()。正確的輸出為 "0102"。
示例 2:輸入:n = 5
輸出:"0102030405"
package com.lau.multithread.printoddeven; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; /** * * 打印零與奇偶數——實現(xiàn)方案——傳統(tǒng)方式 假設有這么一個類: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 構造函數 public void zero(printNumber) { ... } // 僅打印出 0 public void even(printNumber) { ... } // 僅打印出 偶數 public void odd(printNumber) { ... } // 僅打印出 奇數 } 相同的一個 ZeroEvenOdd 類實例將會傳遞給三個不同的線程: 線程 A 將調用 zero(),它只輸出 0 。 線程 B 將調用 even(),它只輸出偶數。 線程 C 將調用 odd(),它只輸出奇數。 每個線程都有一個 printNumber 方法來輸出一個整數。請修改給出的代碼以輸出整數序列 010203040506... ,其中序列的長度必須為 2n。 示例 1: 輸入:n = 2 輸出:"0102" 說明:三條線程異步執(zhí)行,其中一個調用 zero(),另一個線程調用 even(),最后一個線程調用odd()。正確的輸出為 "0102"。 示例 2: 輸入:n = 5 輸出:"0102030405" * * */ class ZeroEvenOdd { private volatile int flag = 0; private volatile int n; private AtomicInteger i; //構造函數 public ZeroEvenOdd(int n) { this.n = n; this.i = new AtomicInteger(1); } //僅打印出 0 public void zero(Runnable printNumber) { for(int k = 0; k < n; k++) { synchronized (this) { try { while(0 != this.flag) { this.wait(); } printNumber.run(); flag = 1; } catch (InterruptedException e) { e.printStackTrace(); } finally { this.notifyAll(); } } } } //僅打印出 偶數 public void even(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { synchronized (this) { try { while(1 != this.flag) { this.wait(); } if(this.i.get() % 2 == 0) { printNumber.accept(this.i.get()); this.i.incrementAndGet(); flag = 0; } else { flag = 2; } this.notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } } } //僅打印出 奇數 public void odd(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { synchronized (this) { try { while(2 != this.flag) { this.wait(); } printNumber.accept(this.i.get()); this.i.incrementAndGet(); flag = 0; this.notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public class PrintOddEvenDemo { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(3); ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(9); threadPool.submit(() -> zeroEvenOdd.zero(() -> System.out.print("0"))); threadPool.submit(() -> zeroEvenOdd.even(t -> System.out.print(String.valueOf(t)))); threadPool.submit(() -> zeroEvenOdd.odd(t -> System.out.print(String.valueOf(t)))); threadPool.shutdown(); } }
package com.lau.multithread.printoddeven; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; /** * * 打印零與奇偶數——實現(xiàn)方案——鎖方式 假設有這么一個類: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 構造函數 public void zero(printNumber) { ... } // 僅打印出 0 public void even(printNumber) { ... } // 僅打印出 偶數 public void odd(printNumber) { ... } // 僅打印出 奇數 } 相同的一個 ZeroEvenOdd 類實例將會傳遞給三個不同的線程: 線程 A 將調用 zero(),它只輸出 0 。 線程 B 將調用 even(),它只輸出偶數。 線程 C 將調用 odd(),它只輸出奇數。 每個線程都有一個 printNumber 方法來輸出一個整數。請修改給出的代碼以輸出整數序列 010203040506... ,其中序列的長度必須為 2n。 示例 1: 輸入:n = 2 輸出:"0102" 說明:三條線程異步執(zhí)行,其中一個調用 zero(),另一個線程調用 even(),最后一個線程調用odd()。正確的輸出為 "0102"。 示例 2: 輸入:n = 5 輸出:"0102030405" * * */ class ZeroEvenOdd2 { private final Lock lock = new ReentrantLock(); final Condition zeroCd = lock.newCondition(); final Condition evenCd = lock.newCondition(); final Condition oddCd = lock.newCondition(); private volatile int flag = 0; private volatile int n; private AtomicInteger i; //構造函數 public ZeroEvenOdd2(int n) { this.n = n; this.i = new AtomicInteger(1); } //僅打印出 0 public void zero(Runnable printNumber) { for(int k = 0; k < n; k++) { try { lock.lock(); while(0 != this.flag) { zeroCd.await(); } printNumber.run(); flag = 1; evenCd.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } //僅打印出 偶數 public void even(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { try { lock.lock(); while(1 != this.flag) { evenCd.await(); } if(this.i.get() % 2 == 0) { printNumber.accept(this.i.get()); this.i.incrementAndGet(); flag = 0; zeroCd.signal(); } else { flag = 2; oddCd.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } //僅打印出 奇數 public void odd(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { try { lock.lock(); while(2 != this.flag) { oddCd.await(); } printNumber.accept(this.i.get()); this.i.incrementAndGet(); flag = 0; zeroCd.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } } public class PrintOddEvenDemo2 { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(3); ZeroEvenOdd2 zeroEvenOdd = new ZeroEvenOdd2(5); threadPool.submit(() -> zeroEvenOdd.zero(() -> System.out.print("0"))); threadPool.submit(() -> zeroEvenOdd.even(t -> System.out.print(String.valueOf(t)))); threadPool.submit(() -> zeroEvenOdd.odd(t -> System.out.print(String.valueOf(t)))); threadPool.shutdown(); } }
package com.lau.multithread.printoddeven; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; /** * * 打印零與奇偶數——實現(xiàn)方案——信號量 假設有這么一個類: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 構造函數 public void zero(printNumber) { ... } // 僅打印出 0 public void even(printNumber) { ... } // 僅打印出 偶數 public void odd(printNumber) { ... } // 僅打印出 奇數 } 相同的一個 ZeroEvenOdd 類實例將會傳遞給三個不同的線程: 線程 A 將調用 zero(),它只輸出 0 。 線程 B 將調用 even(),它只輸出偶數。 線程 C 將調用 odd(),它只輸出奇數。 每個線程都有一個 printNumber 方法來輸出一個整數。請修改給出的代碼以輸出整數序列 010203040506... ,其中序列的長度必須為 2n。 示例 1: 輸入:n = 2 輸出:"0102" 說明:三條線程異步執(zhí)行,其中一個調用 zero(),另一個線程調用 even(),最后一個線程調用odd()。正確的輸出為 "0102"。 示例 2: 輸入:n = 5 輸出:"0102030405" * * */ class ZeroEvenOdd3 { private final Semaphore zeroSp = new Semaphore(1); private final Semaphore evenSp = new Semaphore(0); private final Semaphore oddSp = new Semaphore(0); private volatile int n; private AtomicInteger i; //構造函數 public ZeroEvenOdd3(int n) { this.n = n; this.i = new AtomicInteger(1); } //僅打印出 0 public void zero(Runnable printNumber) { for(int k = 0; k < n; k++) { try { zeroSp.acquire(); printNumber.run(); } catch (InterruptedException e) { e.printStackTrace(); } finally { evenSp.release(); } } } //僅打印出 偶數 public void even(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { try { evenSp.acquire(); if(this.i.get() % 2 == 0) { printNumber.accept(this.i.get()); this.i.incrementAndGet(); zeroSp.release(); } else { oddSp.release(); } } catch (InterruptedException e) { e.printStackTrace(); } } } //僅打印出 奇數 public void odd(Consumer<Integer> printNumber) { for(int k = 0; k < n; k++) { try { oddSp.acquire(); printNumber.accept(this.i.get()); this.i.incrementAndGet(); } catch (InterruptedException e) { e.printStackTrace(); } finally { zeroSp.release(); } } } } public class PrintOddEvenDemo3 { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(3); ZeroEvenOdd3 zeroEvenOdd = new ZeroEvenOdd3(10); threadPool.submit(() -> zeroEvenOdd.zero(() -> System.out.print("0"))); threadPool.submit(() -> zeroEvenOdd.even(t -> System.out.print(String.valueOf(t)))); threadPool.submit(() -> zeroEvenOdd.odd(t -> System.out.print(String.valueOf(t)))); threadPool.shutdown(); } }
輸出:
010203040506070809
上述就是小編為大家分享的leetcode中怎么利用多線程打印零與奇偶數了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。