Title Requirements : Create three threads , Each thread prints separately ABC, And according to ABC Ten times in this order
Problems can be solved in many different ways , Let's use them respectively Condition Wait for wake-up mechanism ,Semaphore Semaphore ,CountDownLatch
atresia ,Thread.join() Methods four ways to achieve the subject requirements .
one , Use one ReentrantLock and Three Condition To achieve :
import java.util.concurrent.locks.Condition; import
java.util.concurrent.locks.ReentrantLock; /** * Title Requirements :ABC Three threads execute sequentially 10 second *
Realization idea : Use one ReentrantLock and Three Condition To achieve */ public class
PrintABCUsingCondition { private static ReentrantLock lock = new
ReentrantLock(); private static Condition conditionA = lock.newCondition();
private static Condition conditionB = lock.newCondition(); private static
Condition conditionC = lock.newCondition(); public void execute(String flag) {
lock.lock(); for (int i = 1 ; i <= 10 ; i++){ if ("A".equals(flag)) print(flag,
conditionA, conditionB); if ("B".equals(flag)) print(flag, conditionB,
conditionC); if ("C".equals(flag)) print(flag, conditionC, conditionA); }
lock.unlock(); } private void print(String name, Condition currentThread,
Condition nextThread) { try{
System.out.println(Thread.currentThread().getName() + "-" + name);
nextThread.signal(); currentThread.await(); }catch (InterruptedException e){
e.printStackTrace(); } } public static void main(String[] args) throws
InterruptedException { PrintABCUsingCondition myTask = new
PrintABCUsingCondition(); new Thread(() -> myTask.execute("A")).start();
// Threads must be ensured A Get it before the other two threads ReentrantLock, So let the main thread sleep a span Thread.sleep(500); new
Thread(() -> myTask.execute("B")).start(); new Thread(() ->
myTask.execute("C")).start(); } }
two , be based on Semaphore Semaphore :
import java.util.concurrent.Semaphore; /** * Title Requirements :ABC Three threads execute sequentially 10 second *
Realization idea : Use one Semaphore Semaphore */ class PrintABCUsingSemaphore { private Semaphore
semaphoreA = new Semaphore(1); private Semaphore semaphoreB = new Semaphore(0);
private Semaphore semaphoreC = new Semaphore(0); private void printA(){
print("A", semaphoreA, semaphoreB); } private void printB(){ print("B",
semaphoreB, semaphoreC); } private void printC(){ print("C", semaphoreC,
semaphoreA); } private void print(String name, Semaphore currentSemaphore,
Semaphore nextSemaphore) { for (int i = 0; i < 10; i++){ try {
currentSemaphore.acquire(); System.out.println(Thread.currentThread().getName()
+" print "+ name); nextSemaphore.release(); } catch (InterruptedException e) {
e.printStackTrace(); } } } public static void main(String[] args) {
PrintABCUsingSemaphore printABC = new PrintABCUsingSemaphore(); new Thread(()
-> printABC.printA()).start(); new Thread(() -> printABC.printB()).start(); new
Thread(() -> printABC.printC()).start(); } }
three , be based on CountDownLatch Locking to achieve :
import java.util.HashMap; import java.util.Map; import java.util.concurrent.*;
/** * Title Requirements :ABC Three threads execute sequentially 10 second * Realization idea : use CountDownLatch To achieve : *
(1) definition dependLatch( Dependent on latch name ),selfLatch( own latch name ) *
(2) Call the dependent first latch of await() method , If it depends on latch of count by 0, Reset the dependent latch And print the required output , Finally, put your own count subtract
* (3)sum Number of times to execute */ public class PrintABCUsingCountDownLatch implements
Runnable { private static Map<String, CountDownLatch> countDownLatchMap = new
HashMap<>(); private String dependLatch; private String selfLatch; private
PrintABCUsingCountDownLatch(String dependLatch, String selfLatch) {
this.dependLatch = dependLatch; this.selfLatch = selfLatch; } @Override public
void run() { for (int i = 0; i < 10; i++) { try {
countDownLatchMap.get(dependLatch).await(); countDownLatchMap.put(dependLatch,
new CountDownLatch(1)); System.out.println(Thread.currentThread().getName() +
":" + selfLatch); countDownLatchMap.get(selfLatch).countDown(); }catch
(InterruptedException e){ e.printStackTrace(); } } } public static void
main(String[] args) { String latchA = "A"; String latchB = "B"; String latchC =
"C"; countDownLatchMap.put(latchA, new CountDownLatch(1));
countDownLatchMap.put(latchB, new CountDownLatch(1));
countDownLatchMap.put(latchC, new CountDownLatch(1));
// Create three threads , But at this time, due to three CountDownLatch Not for 0, So all three threads are blocked Thread threadA = new
Thread(new PrintABCUsingCountDownLatch(latchC, latchA)); Thread threadB = new
Thread(new PrintABCUsingCountDownLatch(latchA, latchB)); Thread threadC = new
Thread(new PrintABCUsingCountDownLatch(latchB, latchC)); threadA.start();
threadB.start(); threadC.start(); //latchC Blocked
latchA; call latchC of countDown() method , Let's go first latchC by 0, send latchA Run first
countDownLatchMap.get(latchC).countDown(); }
four , use Thread.join() Method :
/** * Title Requirements :ABC Three threads execute sequentially 10 second * Realization idea : use Thread.join() Method */ public class
PrintABCUsingJoin { public static void main(String[] args) { Thread t0 = new
Thread(new Work((null))); Thread t1 = new Thread(new Work((t0))); Thread t2 =
new Thread(new Work((t1))); t0.start(); t1.start(); t2.start(); } } class Work
implements Runnable { private Thread beforeThread; public Work(Thread
beforeThread) { this.beforeThread = beforeThread; } @Override public void run()
{ // Call the previous thread join method if(beforeThread != null) { try{ beforeThread.join(); }catch
(InterruptedException e) { e.printStackTrace(); } } System.out.println(" Current thread :"
+ Thread.currentThread().getName()); } }
Technology
Daily Recommendation