多线程交替打印ABC
面渣的十二种写法
https://blog.csdn.net/ZHAOJING1234567/article/details/89462442
https://blog.csdn.net/qq_44373419/article/details/139796030
Semaphore
semaphore介绍:https://blog.csdn.net/2501_92540271/article/details/149043567
一般写法
package org.example.printABC;import java.util.concurrent.Semaphore;public class SemaphorePrint {public static final Semaphore ASemaphore = new Semaphore(1);public static final Semaphore BSemaphore = new Semaphore(0);public static final Semaphore CSemaphore = new Semaphore(0);public static void main(String[] args){Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 3; i++) {try {ASemaphore.acquire();System.out.println(Thread.currentThread().getName() + " 打印A");BSemaphore.release();} catch (InterruptedException e) {throw new RuntimeException(e);}}}}, "thread1");Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 3; i++) {try {BSemaphore.acquire();System.out.println(Thread.currentThread().getName() + " 打印B");CSemaphore.release();} catch (InterruptedException e) {throw new RuntimeException(e);}}}}, "thread2");Thread thread3 = new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 3; i++) {try {CSemaphore.acquire();System.out.println(Thread.currentThread().getName() + " 打印C");ASemaphore.release();} catch (InterruptedException e){throw new RuntimeException(e);}}}}, "thread3");thread1.start();thread2.start();thread3.start();}
}
更简洁的写法
package org.example.printABC;import java.util.concurrent.Semaphore;public class SemaphorePrintSimple {private final Semaphore semA = new Semaphore(1);private final Semaphore semB = new Semaphore(0);private final Semaphore semC = new Semaphore(0);public static int n = 3;public static void main(String[] args) {SemaphorePrintSimple sem = new SemaphorePrintSimple();new Thread(()->sem.print('A', sem.semA, sem.semB)).start();new Thread(()->sem.print('B', sem.semB, sem.semC)).start();new Thread(()->sem.print('C', sem.semC, sem.semA)).start();}public void print(char ch, Semaphore cur, Semaphore next) {for (int i = 0; i < n; i++) {try {cur.acquire();System.out.println(Thread.currentThread().getName() + ": " + ch);next.release();} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
ReentrantLock
lock
package org.example.printABC;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockPrintSimple {private static Lock lock = new ReentrantLock();private static int state = 0;static class MyThread extends Thread {private int n;private int orderNum;private char c;public MyThread(int n, char c, int orderNum) {this.n = n;this.c = c;this.orderNum = orderNum;}@Overridepublic void run() {// 不断轮训for (int i = 0; i < n; ) {// 有资格执行lock.lock();try{// 是否能真正执行if(state % 3 == orderNum){System.out.println(Thread.currentThread().getName() + ": "+ c);i++;state++;}} finally {lock.unlock();}}}}public static void main(String[] args) {new MyThread(3, 'A', 0).start();new MyThread(3, 'B', 1).start();new MyThread(3, 'C', 2).start();}
}
package org.example.printABC;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockPrint {private static Lock lock = new ReentrantLock();private static int state = 0;public static void main(String[] args) {new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 10; ) {lock.lock();try {if (state % 3 == 0){System.out.println(Thread.currentThread().getName() + ": A");i++;state ++;}} finally {lock.unlock();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 10; ) {lock.lock();try {if (state % 3 == 1){System.out.println(Thread.currentThread().getName() + ": B");i++;state ++;}} finally {lock.unlock();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 10; ) {lock.lock();try {if (state % 3 == 2){System.out.println(Thread.currentThread().getName() + ": C");i++;state ++;}} finally {lock.unlock();}}}}).start();}
}
lock+condition
package org.example.printABC;import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockConditionPrint {private static int state = 0;private static Lock lock = new ReentrantLock();private static Condition conditionA = lock.newCondition();private static Condition conditionB = lock.newCondition();private static Condition conditionC = lock.newCondition();private void print(char c, int n, int orderNum, Condition cur, Condition next) {for (int i = 0; i < n; ) {lock.lock();try{while (state % 3 != orderNum){cur.await();}System.out.println(Thread.currentThread().getName() + ": " + c);state++;i++;next.signal();} catch (InterruptedException e){throw new RuntimeException(e);} finally {lock.unlock();}}}public static void main(String[] args) {//static方法引用非static方法ReentrantLockConditionPrint rlc = new ReentrantLockConditionPrint();new Thread(()->rlc.print('A',10, 0, conditionA, conditionB)).start();new Thread(()->rlc.print('B',10, 1, conditionB, conditionC)).start();new Thread(()->rlc.print('C',10, 2, conditionC, conditionA)).start();}
}
Sync
package org.example.printABC;public class syncPrint {private static Object lock = new Object();private static int state = 0;public void print(char c,int n, int orderNum){for (int i = 0; i < n; ) {try{synchronized (lock){// 必须是while不能是ifwhile (state % 3 != orderNum){lock.wait();}System.out.println(Thread.currentThread().getName() + ": " + c);state++;i++;// notifyAll要写在syn块内,不要写到finally里lock.notifyAll();}} catch (InterruptedException e) {throw new RuntimeException(e);}}}public static void main(String[] args) {syncPrint syn = new syncPrint();new Thread(()->syn.print('A',10, 0)).start();new Thread(()->syn.print('B',10, 1)).start();new Thread(()->syn.print('C',10, 2)).start();}
}