三个线程分别打印 A,B,C
三个线程分别打印 A,B,C,要求这三个线程一起运行,打印 n 次,输出形如“ABCABCABC….”的字符串
使用Lock
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
| import java.util.concurrent.locks.ReentrantLock;
public class ABC { static class ABCPrinter { private final int times; private volatile int state; private final ReentrantLock lock;
public ABCPrinter(int times) { this.times = times; this.lock = new ReentrantLock(); }
public void print(Character key, int num) { int current = 0; while (current < times) { lock.lock(); if (state % 3 == num) { System.out.println(key); state++; current++; } lock.unlock(); } } }
public static void main(String[] args) throws InterruptedException { final ABCPrinter abcPrinter = new ABCPrinter(10); Thread a = new Thread(() -> abcPrinter.print('A', 0)); a.start(); Thread b = new Thread(() -> abcPrinter.print('B', 1)); b.start(); Thread c = new Thread(() -> abcPrinter.print('C', 2)); c.start(); a.join(); b.join(); c.join(); } }
|
使用wait/notify
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
| public class ABC { static class ABCPrinter { private final static Object LOCK = new Object(); private final int times; private volatile int state;
public ABCPrinter(int times) { this.times = times; }
public void print(Character key, int num) { int current = 0; while (current < times) { synchronized(LOCK){ while (state % 3 != num) { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } state++; current++; System.out.println(key); LOCK.notifyAll(); } } } }
public static void main(String[] args) throws InterruptedException { final ABCPrinter abcPrinter = new ABCPrinter(10); Thread a = new Thread(() -> abcPrinter.print('A', 0)); a.start(); Thread b = new Thread(() -> abcPrinter.print('B', 1)); b.start(); Thread c = new Thread(() -> abcPrinter.print('C', 2)); c.start(); a.join(); b.join(); c.join(); } }
|
两个线程交替打印0~100的奇偶数
两个线程交替打印0~100的奇偶数
使用wait/notify
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
| public class OddEvenPrinter {
static class Printer {
private final static Object LOCK = new Object(); private volatile int state = 0; private final int times;
public Printer(int times) { this.times = times; }
public void print(int mod) { while (state < times) { synchronized (LOCK) { while (state % 2 != mod) { try { LOCK.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } System.out.println("name: " + Thread.currentThread().getName() + ", number: " + state++); LOCK.notify(); } } }
}
public static void main(String[] args) { Printer printer = new Printer(100); new Thread(() -> printer.print(0)).start(); new Thread(() -> printer.print(1)).start(); }
}
|
通过N个线程顺序循环打印从0至100
通过N个线程顺序循环打印从0至100
使用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
| import java.util.concurrent.Semaphore;
public class NThreadPrinter { private final int limit = 100; private final int threadCount; private volatile int state = 0; private final Semaphore[] semaphores;
public NThreadPrinter(int threadCount) { this.threadCount = threadCount; semaphores = new Semaphore[threadCount]; for (int i = 0; i < threadCount; i++) { semaphores[i] = new Semaphore(i == 0 ? 1 : 0); } }
public void print(int threadId) { while (true) { try { semaphores[threadId].acquire(); if (state > limit) { semaphores[(threadId + 1) % threadCount].release(); return; } System.out.println("线程 " + threadId + " 打印:" + state++); semaphores[(threadId + 1) % threadCount].release(); } catch (InterruptedException e) { e.printStackTrace(); } } }
public static void main(String[] args) { int N = 3; NThreadPrinter printer = new NThreadPrinter(N);
for (int i = 0; i < N; i++) { final int threadId = i; new Thread(() -> printer.print(threadId)).start(); } } }
|