前言:学习JUC包后,通过leetcode多线程的题目练习一下,1195-交替打印字符串
Semaphore
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class FizzBuzz {
private int n; private Semaphore number = new Semaphore(1); private Semaphore fizz = new Semaphore(0); private Semaphore buzz = new Semaphore(0); private Semaphore fizzbuzz = new Semaphore(0);
public FizzBuzz(int n) { this.n = n; }
public void fizz(Runnable printFizz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 != 0) { fizz.acquire(); printFizz.run(); number.release(); } }
}
public void buzz(Runnable printBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 5 == 0 && i % 3 != 0) { buzz.acquire(); printBuzz.run(); number.release(); } }
}
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 15 == 0) { fizzbuzz.acquire(); printFizzBuzz.run(); number.release(); } }
}
public void number(IntConsumer printNumber) throws InterruptedException { for (int i = 1; i <= n; i++) { number.acquire(); if (i % 15 == 0) { fizzbuzz.release(); } else if (i % 3 == 0) { fizz.release(); } else if (i % 5 == 0) { buzz.release(); } else { printNumber.accept(i); number.release(); } } } }
|
cyclicBarrier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public class FizzBuzz {
private int n; private CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
public FizzBuzz(int n) { this.n = n; }
public void fizz(Runnable printFizz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 != 0) { printFizz.run(); } try { cyclicBarrier.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
}
public void buzz(Runnable printBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 5 == 0 && i % 3 != 0) { printBuzz.run(); } try { cyclicBarrier.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
}
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 15 == 0) { printFizzBuzz.run(); } try { cyclicBarrier.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); }
}
}
public void number(IntConsumer printNumber) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 != 0 && i % 5 != 0) { printNumber.accept(i); } try { cyclicBarrier.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } }
|
ReentrantLock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public class FizzBuzz {
private int n; private AtomicInteger index = new AtomicInteger(1); private ReentrantLock lock = new ReentrantLock();
public FizzBuzz(int n) { this.n = n; }
public void fizz(Runnable printFizz) throws InterruptedException { while (index.get() <= n) { lock.lock(); if (index.get() <= n && index.get() % 3 == 0 && index.get() % 5 != 0) { printFizz.run(); index.getAndIncrement(); } lock.unlock(); } }
public void buzz(Runnable printBuzz) throws InterruptedException { while (index.get() <= n) { lock.lock(); if (index.get() <= n && index.get() % 5 == 0 && index.get() % 3 != 0) { printBuzz.run(); index.getAndIncrement(); } lock.unlock(); } }
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { while (index.get() <= n) { lock.lock(); if (index.get() <= n && index.get() % 15 == 0) { printFizzBuzz.run(); index.getAndIncrement(); } lock.unlock(); } }
public void number(IntConsumer printNumber) throws InterruptedException { while (index.get() <= n) { lock.lock(); if (index.get() <= n && index.get() % 3 != 0 && index.get() % 5 != 0) { printNumber.accept(index.get()); index.getAndIncrement(); } lock.unlock(); } } }
|
synchronized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public class FizzBuzz {
private int n; private AtomicInteger index = new AtomicInteger(1);
public FizzBuzz(int n) { this.n = n; }
public void fizz(Runnable printFizz) throws InterruptedException { while (index.get() <= n) { synchronized (this) { if (index.get() <= n && index.get() % 3 == 0 && index.get() % 5 != 0) { printFizz.run(); index.getAndIncrement(); } } } }
public void buzz(Runnable printBuzz) throws InterruptedException { while (index.get() <= n) { synchronized (this) { if (index.get() <= n && index.get() % 5 == 0 && index.get() % 3 != 0) { printBuzz.run(); index.getAndIncrement(); } } } }
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { while (index.get() <= n) { synchronized (this) { if (index.get() <= n && index.get() % 15 == 0) { printFizzBuzz.run(); index.getAndIncrement(); } } } }
public void number(IntConsumer printNumber) throws InterruptedException { while (index.get() <= n) { synchronized (this) { if (index.get() <= n && index.get() % 3 != 0 && index.get() % 5 != 0) { printNumber.accept(index.get()); index.getAndIncrement(); } } } } }
|