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

How does Java make multithreads execute sequentially?

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "Java how to make multithreaded execution in order", detailed content, clear steps, and proper handling of details. I hope this article "Java how to make multithreaded execution in sequence" can help you solve your doubts.

Specify the order in the child thread through the join () method

The current thread is "blocked" by the join () method, waiting for the specified thread to finish execution and continue execution. For example: in the thread thread2, add a sentence thread1.join (), the meaning is that the current thread 2 runs to this line of code will enter the blocking state, until the thread thread1 execution is completed, the thread thread2 will not continue to run, which ensures the running order of thread thread1 and thread thread2.

Public class ThreadJoinDemo {public static void main (String [] args) throws InterruptedException {final Thread thread1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("Open the refrigerator!") Final Thread thread2 = new Thread (new Runnable () {@ Override public void run () {try {thread1.join ();} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("take out a bottle of milk!") Final Thread thread3 = new Thread (new Runnable () {@ Override public void run () {try {thread2.join ();} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("close the refrigerator!") The order of the following three lines of code can be adjusted at will, and the running result of the program will not be affected, because we have specified the running order in the child thread through the "join () method". Thread3.start (); thread2.start (); thread1.start ();}}

Running result:

Open the fridge!

Take out a bottle of milk!

Close the fridge!

Specify the order in the main thread through the join () method

Briefly talk about the difference between the child thread and the main thread, the child thread refers to the code that occurs within the Thread, and the main thread refers to the code that occurs in the main function. We can use the join () method in the main function to let the main thread block waiting to achieve the purpose of specified sequence execution.

Public class ThreadMainJoinDemo {public static void main (String [] args) throws InterruptedException {final Thread thread1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("Open the refrigerator!") Final Thread thread2 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("take out a bottle of milk!") Final Thread thread3 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("close the refrigerator!") ); thread1.start (); thread1.join (); thread2.start (); thread2.join (); thread3.start ();}}

Output result:

Open the fridge!

Take out a bottle of milk!

Close the fridge!

Realized by countdown timer CountDownLatch

CountDownLatch provides more flexible control through counters, so that as long as the current thread detects that the counter is 0, it can execute down regardless of whether the corresponding thread is finished.

Public class ThreadCountDownLatchDemo {private static CountDownLatch countDownLatch2 = new CountDownLatch (1); private static CountDownLatch countDownLatch3 = new CountDownLatch (1); public static void main (String [] args) {final Thread thread1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("Open the refrigerator!") ; countDownLatch2.countDown ();}}); final Thread thread2 = new Thread (new Runnable () {@ Override public void run () {try {countDownLatch2.await (); System.out.println ("take out a bottle of milk!") ; countDownLatch3.countDown ();} catch (InterruptedException e) {e.printStackTrace ();}) Final Thread thread3 = new Thread (new Runnable () {@ Override public void run () {try {countDownLatch3.await (); System.out.println ("close the refrigerator!") ;} catch (InterruptedException e) {e.printStackTrace ();}); / / the order of the following three lines of code can be adjusted at will, and the running result of the program will not be affected by thread3.start (); thread1.start (); thread2.start ();}}

Output result:

Open the fridge!

Take out a bottle of milk!

Close the fridge!

By creating a single thread pool, newSingleThreadExecutor ()

Single-threaded thread pool (newSingleThreadExecutor) has the advantage of performing all tasks serially.

Public class ThreadPoolDemo {static ExecutorService executorService = Executors.newSingleThreadExecutor (); public static void main (String [] args) {final Thread thread1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("Open the refrigerator!") Final Thread thread2 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("take out a bottle of milk!") Final Thread thread3 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("close the refrigerator!") ;}}); executorService.submit (thread1); executorService.submit (thread2); executorService.submit (thread3); executorService.shutdown (); / / remember to close the thread pool}}

Output result:

Open the fridge!

Take out a bottle of milk!

Close the fridge!

After reading this, the article "how Java allows multithreading to execute sequentially" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report