Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using threads to print sequentially

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "using threads to print in order". In daily operation, I believe that many people have doubts about using threads to print in order. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "printing in order with threads"! Next, please follow the editor to study!

Summary: all solutions are an idea. No matter which way you use, you must execute the first thread at the beginning, then set the condition to satisfy the second execution, but neither the first nor third threads can execute, and only the first thread can give this condition, and then set the condition to satisfy the third execution while the first and second threads cannot execute, and only if the second thread executes successfully can the condition be given.

Solution 1: Synchronized lock and control variable

Public class Foo {

/ / Control variable

Private int flag = 0

/ / define the Object object as a lock

Private Object lock = new Object ()

Public Foo () {

}

Public void first (Runnable printFirst) throws InterruptedException {

Synchronized (lock) {

/ / if the flag is not 0, let the first thread wait. The while loop controls the first thread to stay in the while code block if it is not satisfied with the living conditions to prevent it from jumping in midway. Execute the following code, and the rest of the thread while loop is the same.

While (flag! = 0) {

Lock.wait ()

}

/ / printFirst.run () outputs "first". Do not change or remove this line.

PrintFirst.run ()

/ / define the member variable as 1

Flag = 1

/ / Wake up all remaining threads

Lock.notifyAll ()

}

}

Public void second (Runnable printSecond) throws InterruptedException {

Synchronized (lock) {

/ / if the member variable is not 1, let No. 2 wait.

While (flag! = 1) {

Lock.wait ()

}

/ / printSecond.run () outputs "second". Do not change or remove this line.

PrintSecond.run ()

/ / if the member variable is 1, it means that the first thread has just finished executing, so execute second and change the member variable to 2

Flag = 2

/ / Wake up all remaining threads

Lock.notifyAll ()

}

}

Public void third (Runnable printThird) throws InterruptedException {

Synchronized (lock) {

/ / if flag is not equal to 2, it will be waiting all the time

While (flag! = 2) {

Lock.wait ()

}

/ / printThird.run () outputs "third". Do not change or remove this line.

/ / if the member variable is 2, it means that the second thread has just finished execution, so execute third and change the member variable to 0

PrintThird.run ()

Flag = 0

Lock.notifyAll ()

}

}

}

Solution 2: CountDownLatch

Public class Foo {

/ / declare two CountDownLatch variables

Private CountDownLatch countDownLatch01

Private CountDownLatch countDownLatch02

Public Foo () {

/ / initialize each CountDownLatch with a value of 1, indicating that there is a thread that waits after it has finished executing.

CountDownLatch01 = new CountDownLatch (1)

CountDownLatch02 = new CountDownLatch (1)

}

Public void first (Runnable printFirst) throws InterruptedException {

/ / currently, only the first thread does not have any hindrance, and the other two threads are in the waiting phase

/ / printFirst.run () outputs "first". Do not change or remove this line.

PrintFirst.run ()

/ / the thread waiting due to the call to the countDownCatch01.await () is not executed until the count in the CountDownLatch01 is 0.

CountDownLatch01.countDown ()

}

Public void second (Runnable printSecond) throws InterruptedException {

/ / you can only pass if countDownLatch01 is 0, otherwise it will always block.

CountDownLatch01.await ()

/ / printSecond.run () outputs "second". Do not change or remove this line.

PrintSecond.run ()

/ / the thread waiting due to the call to the countDownCatch02.await () is not executed until the count in the CountDownLatch02 is 0.

CountDownLatch02.countDown ()

}

Public void third (Runnable printThird) throws InterruptedException {

/ / you can only pass if countDownLatch02 is 0, otherwise it will always block.

CountDownLatch02.await ()

/ / printThird.run () outputs "third". Do not change or remove this line.

PrintThird.run ()

}

}

Solution 3: Semaphore (semaphore)

Semaphore is similar to CountDownLatch, except that the value of Semaphore can be released after it is obtained, and it is not reduced to the end like CountDownLatch.

After the thread that gets the Semaphore has processed its logic, you can call its Release () function to re-add its counter to 1 so that other blocked threads can be called.

Public class Foo03 {

/ / declare two Semaphore variables

Private Semaphore spa,spb

Public Foo03 () {

/ / initialize the reason why Semaphore is 0: if the Semaphore is zero, if another thread calls (acquire) the Semaphore, it will block, and you can control the execution of second and third threads.

Spa = new Semaphore (0)

Spb = new Semaphore (0)

}

Public void first (Runnable printFirst) throws InterruptedException {

/ / printFirst.run () outputs "first". Do not change or remove this line.

PrintFirst.run ()

/ / another thread can only call (acquire) until the first thread releases the Semaphore with a Semapore value of 1.

Spa.release ()

}

Public void second (Runnable printSecond) throws InterruptedException {

/ / only if spa is 1, acquire can be executed. If it is 0, it will cause blocking.

Spa.acquire ()

/ / printSecond.run () outputs "second". Do not change or remove this line.

PrintSecond.run ()

Spb.release ()

}

Public void third (Runnable printThird) throws InterruptedException {

/ / only a spb of 1 can pass. If it is 0, it will block

Spb.acquire ()

/ / printThird.run () outputs "third". Do not change or remove this line.

PrintThird.run ()

}

}

At this point, the study of "printing in sequence with threads" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report