Usage of Java Thread join ()

Source: Internet
Author: User

In Java thread, the Join () method is basically the code that calls the thread of the modified method to complete the contents of the Run method after the execution of the join () method. Example: Java code
  1. Class Threadtestera implements Runnable {
  2. private int counter;
  3. @Override
  4. public Void Run () {
  5. While (counter <= ) {
  6. System.out.print ("Counter =" + Counter + ");
  7. counter++;
  8. }
  9. System.out.println ();
  10. }
  11. }
  12. Class Threadtesterb implements Runnable {
  13. private int i;
  14. @Override
  15. public Void Run () {
  16. While (i <= ) {
  17. System.out.print ("i =" + i + "");
  18. i++;
  19. }
  20. System.out.println ();
  21. }
  22. }
  23. Public class Threadtester {
  24. public static void Main (string[] args) throws interruptedexception {
  25. Thread T1 = new Thread (new Threadtestera ());
  26. Thread t2 = new Thread (new Threadtesterb ());
  27. T1.start ();
  28. T1.join (); //wait T1 to be finished
  29. T2.start ();
  30. T2.join (); //In the This program, this is May removed
  31. }
  32. }


If you remove T1.join () After this statement, the output of the book is chaotic, and after this statement, it will be output sequentially, in a sense to achieve synchronization.

Transferred from: http://auguslee.iteye.com/blog/1292203

Usage of Java Thread join ()

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.