Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the main thread waiting for the child thread to finish execution

Shulou Source: shulou.com Published: 2022-06-03 06:37:15 11月15日 Update

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!

Tags: Thread method method name situation run intuitive content more knowledge result blocking practical consistent learned and then two code at the same time dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi macOS Shulou Tech Info MariaDB