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 to implement the main thread waiting for the child thread to finish execution

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to implement the main thread waiting for the child thread to finish execution". In the operation of the actual case, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Public static void main (String [] args) {Thread T1 = new Thread (new Runnable () {@ Override public void run () {try {System.out.println ("thread" + Thread.currentThread (). GetName () + "start to run.") Thread.sleep (2000); System.out.println ("thread" + Thread.currentThread (). GetName () + "done.");} catch (Exception e) {e.printStackTrace () }); System.out.println ("main start to run."); t1.start (); System.out.println ("main done.");}

By default, thread T1 does not end immediately, and the result of execution is

We want to wait for the child thread to finish running after the main thread starts, and the main thread will continue to execute.

Considering that the child thread is a time-consuming blocking operation, we need to let the child thread run at the end of the program before executing the main thread. Here are the following solutions

①, t1.join ()

②, while (t1.isAlive ())

③, while (Thread.activeCount () > 1)

④ 、 CountDownLatch

Method 1: t1.start (), followed by t1.join ()

Method 2 to determine whether the child thread is still alive or not

Method 3 determines whether the active thread is greater than 1

Method 4 is implemented through the synchronization tool class CountDownLatch

This approach requires the CountDownLatch object to invade the code of the thread's run () method. The final implementation result is consistent with the previous solutions.

CountDownLatch blocks the main thread through the await () method, waits for other threads to finish running, releases the lock through the countDown () method, and the main thread starts execution.

Summary:

One or two methods need to know the name of the thread, when there are many threads executing at the same time, sometimes we can not directly know the name of each thread, these two methods are very rare.

The fourth method, which can also be used in the case of multithreading, is to set CountDownLatch latch = new CountDownLatch (n) to specify the number of threads to wait.

The third method does not need to know the name of the thread and the number of threads, and is intuitive to use.

This is the end of the content of "how to implement the main thread waiting for the child thread to finish execution". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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