JAVA Threading Basics Summary--join yield

Source: Internet
Author: User

Join threads The thread that executes the method, allowing the current county to execute another thread after execution

Package Org.famous.unyielding.current.jooin;public class Threadjoinclient {public static void main (string[] args) { Thread t = new Thread (new Runnable () {@Overridepublic void run () {System.err.println ("Hello World");}); try {t.join ();} catch (Interruptedexception e) {e.printstacktrace ();} T.start (); System.err.println ("main thread");}}

The result of adding joins and not adding joins is that the order of the Hello World output is inconsistent. Add a join so Hello World will take precedence

The difference between the Join (long xx) Sleep (long xx). Join will release the lock, but sleep will not



The first method of validating join (Long xx) is the second proof join lock mechanism:

Package Org.famous.unyielding.current.jooin;public class Threadjoinclient {public static void main (string[] args) {  Thread t = new Thread (new Runnable () {@Overridepublic void run () {try {thread.sleep (4000);} catch (Interruptedexception e) {E.printstacktrace ();} System.err.println ("Hello World");}); T.start (); try {t.join (a);} catch (Interruptedexception e) {e.printstacktrace ();} System.err.println ("main thread");}}


Must start again join otherwise invalid.! See the source of the join in detail:

    1. 0 )  {  

    2.             < Span style= "Color:rgb (127, 0, 85); Font-weight:bold; " >while   (isAlive ())  {  

    3.                  wait (0 );   

    4.              }  

    5.          } else  { 


Lock mechanism for join:

The above code depends on who called the join is the main thread so stopping is the main thread.


Essential question: What is the difference between join (1000) and sleep (1000)? Having a child thread join or let the main thread sleep essentially can meet our requirements. As long as we have a good estimate of time.


But if we don't add time. Or to verify the lock problem.


Is whether the join will let the thread that called it release the lock.


The 1.join parameter does not wait until the specified time, and if the thread finishes running prematurely, it will end up waiting prematurely.

2.

Public class mytest {public static void main (String[] args)  {SonRun  sonrun = new sonrun (); Thread thread = new thread (New joinrunn (Sonrun)); Thread.Start (); Thread thread2 = new thread (New threerrun (Sonrun)); Thread2.start ();}}   Do I call join  to release the lock class joinrunn implements runnable {private sonrun  Sonrun;public joinrunn (Sonrun sonrun)  {super (); this.sonrun = sonrun;} @Overridepublic  void run ()  {synchronized  (Sonrun)  {system.err.println ("Join run  get the sonrun lock "); Try {sonrun.start ();/***  zhongdian **///  Sonrun.join (3000); Thread.Sleep (3000); System.err.println ("Join run will execute after sonrun");for  (int i =  0; i < integer.max_value; i++)  {string newstring = nEw string (); Math.random ();}}  catch  (interruptedexception e)  {e.printstacktrace ();}}} class sonrun extends thread {@Overridepublic  void run ()  {long begin  = system.currenttimemillis (); Try {thread.sleep (2000);}  catch  (interruptedexception e)  {e.printstacktrace ();} Long end = system.currenttimemillis (); System.err.println (Begin - end);}} Class threerrun implements runnable {private sonrun sonrun;public threerrun (Sonrun sonrun)  {this.sonrun = sonrun;} @Overridepublic  void run ()  {synchronized  (Sonrun)  {system.err.println ("What the  time shold i fucked be execute ");}}}

The result of this code operation is Joinrunn

Sonrun.join (3000);

Thread.Sleep (3000);

The results are different for each operation. So be wary of join is the problem of releasing the lock.

join ()  if any executing thread t1 calls  join ()  on T2 i.e;  t2.join ()  immediately T1 would enter into the waiting state until T2 completes its execution. &NBSP;T1 calls the T2 join method, the T1 will wait until the end of the time or the end of the T1 run. Since it is wait, where is the sync or lock?


  1. yield()

  2. join()

  3. sleep()

  4. yield()Method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same Priority to execute. If There is no waiting thread or all the waiting threads has a lower priority then the same thread would continue its exec Ution. The yielded thread when it'll get the chance for execution are decided by the thread scheduler whose behavior is vendor D Ependent.


  5. sleep()Based on requirement we can make a thread to is in sleeping state for a specified period of time (Hope not much explan ation required for our favorite method).





The following about yield

Package Org.famous.unyielding.current.jooin;public class Yieldthreadclient {public static void main (string[] args) { Thread thread1 = new Thread (new Yieldrunn ()); Thread thread2 = new Thread (new Yieldrunn ()); Thread1.start (); Thread2.start ();}} Class Yieldrunn implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.err.println (i); I F (i = =) {Thread.yield ();}}}}


JAVA Thread Basics Rollup--join yield

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.