Java implementation main thread waits for child threads

Source: Internet
Author: User

This article describes the implementation of two main thread-waiting sub-threads, which are described in 5 threads:

1. Using the thread's join () method, the join () method blocks the main thread from continuing down execution.

2, using the Java.util.concurrent in the Countdownlatch, is a countdown counter. The initial value of the countdown is set first, each time the countdown () method is called, minus one, and his await () method blocks the current process until it counts down to 0.

The Join method code is as follows:

[Java]View PlainCopy
  1. Package com.test.thread;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Public class MyThread extends Thread
  5. {
  6. Public MyThread (String name)
  7. {
  8. this.setname (name);
  9. }
  10. @Override
  11. public Void Run ()
  12. {
  13. System.out.println (this.getname () + "staring ...");
  14. System.out.println (this.getname () + "End ...");
  15. }
  16. /** 
  17. * @param args
  18. */
  19. public static void Main (string[] args)
  20. {
  21. SYSTEM.OUT.PRINTLN ("main thread starting ...");
  22. list<mythread> list = new arraylist<mythread> ();
  23. For (int i = 1; I <= 5; i++)
  24. {
  25. MyThread my = new MyThread ("Thrad" + i);
  26. My.start ();
  27. List.add (my);
  28. }
  29. Try
  30. {
  31. For (MyThread my:list)
  32. {
  33. My.join ();
  34. }
  35. }
  36. catch (interruptedexception e)
  37. {
  38. E.printstacktrace ();
  39. }
  40. SYSTEM.OUT.PRINTLN ("Main thread end ...");
  41. }
  42. }

The results of the operation are as follows:

Main thread starting ...
Thrad 2 Staring ...
Thrad 2 End ...
Thrad 4 Staring ...
Thrad 4 End ...
Thrad 1 Staring ...
Thrad 1 End ...
Thrad 3 Staring ...
Thrad 3 End ...
Thrad 5 Staring ...
Thrad 5 End ...
Main thread End ...
The Countdownlatch mode code is as follows:

[Java]View PlainCopy
  1. Package com.test.thread;
  2. Import Java.util.concurrent.CountDownLatch;
  3. Public class MyThread2 extends Thread
  4. {
  5. private Countdownlatch count;
  6. Public MyThread2 (countdownlatch count, String name)
  7. {
  8. This.count = count;
  9. this.setname (name);
  10. }
  11. @Override
  12. public Void Run ()
  13. {
  14. System.out.println (this.getname () + "staring ...");
  15. System.out.println (this.getname () + "End ...");
  16. This.count.countDown ();
  17. }
  18. /** 
  19. * @param args
  20. */
  21. public static void Main (string[] args)
  22. {
  23. SYSTEM.OUT.PRINTLN ("main thread starting ...");
  24. Countdownlatch count = new Countdownlatch (5);
  25. For (int i = 1; I <= 5; i++)
  26. {
  27. MyThread2 my = new MyThread2 (count, "Thread" + i);
  28. My.start ();
  29. }
  30. Try
  31. {
  32. Count.await ();
  33. }
  34. catch (interruptedexception e)
  35. {
  36. E.printstacktrace ();
  37. }
  38. SYSTEM.OUT.PRINTLN ("Main thread end ...");
  39. }
  40. }


The results of the operation are as follows:

Main thread starting ...
Thread 2 Staring ...
Thread 2 End ...
Thread 4 Staring ...
Thread 4 End ...
Thread 1 Staring ...
Thread 1 End ...
Thread 3 Staring ...
Thread 3 End ...
Thread 5 Staring ...
Thread 5 End ...
Main thread End ...

Java implementation main thread waits for child threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.