Join methods in Java Multi-threading

Source: Internet
Author: User

Create a new thread with the following code:

1  Packagecom.thread.test;2 3  Public classMyThreadextendsThread {4     PrivateString name;5      PublicMyThread (String name) {6          This. Name =name;7     }8 @Override9      Public voidrun () {Ten          for(inti = 0; I < 100; i++) { OneSystem.out.println (name+ "[" +i+ "]"); A         } -         Super. Run (); -     } the}

After that, create a new test class with the following code:

1  Packagecom.thread.test;2 /*3 * 0-50 executes the main thread, 50-100 executes a thread, and the a thread executes completely before continuing the main thread4  */5  Public classthreaddemo{6      Public Static voidMain (string[] args) {7MyThread T =NewMyThread ("A");8 T.start ();9          for(inti = 0; I < 100; i++) {Ten             if(i>50) { One                 Try { A T.join (); -}Catch(interruptedexception e) { - e.printstacktrace (); the                 } -             } -System.out.println ("Main thread" + "[" +i+ "]"); -         } +     } -}

The following is an explanation of the join method in thread in the Java Platform SE8 API:

 Public Final voidJoinLongMillis)throwsInterruptedexceptionwaits at the most millis milliseconds for  ThisThread to die. A Timeout of 0means to wait forever. This implementation uses a loop of This. Wait calls conditioned on This. isAlive. As a thread terminates the This. Notifyall method is invoked. It is recommended this applications not use wait, notify, or Notifyall on Thread instances. Parameters:millis-The time to wait in milliseconds throws:illegalargumentexception-ifThe value of Millis is negative interruptedexception-ifAny thread have interrupted the current thread. The interrupted status of the current thread was cleared when ThisException is thrown.

Join methods in Java Multi-threading

Related Article

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.