Java multiple-thread join (), sleep (), yield (), wait () function __

Source: Internet
Author: User
Join ()

When the thread T1 calls T2.join (), T1 keeps the lock object, waits for T2 execution, and T1 into the executable state;

package thread;

public class Joinrunnable implements Runnable {
	
	Thread t1, T2;
	
	Public joinrunnable () {
		T1 = new Thread (this);
		T1.setname ("T1");
		t2 = new Thread (this);
		T2.setname ("T2");
	}

	@Override public
	void  Run () {//If the synchronized lock tag is added, even if the T1 thread calls T2.join (), T2 cannot perform
             as expected because the join does not release the lock System.out.println (Thread.CurrentThread (). GetName ()); 
             
             if (Thread.CurrentThread () ==t1) {	////////
            	 try {
            		System.out.println ("T2.join ();");
					T2.join ();
				catch (Interruptedexception e) {
					e.printstacktrace ();
				}
             }
             
             for (int i = 0; i < i++) {System.out.println (
            	 thread.currentthread (). GetName ()); 
			}

	public static void Main (string[] args) {
		joinrunnable runnable = new joinrunnable ();
		Runnable.t1.start ();
		Runnable.t2.start ();
	}
	

Run Result:

T1
T2
t2.join ();
T2
T2
T2
T2
T2
T2
T2 T2 T2 T2 t1 T1 T1 T1 T1
T1
t1
t1
T1
T1


--------------------------------------------------------------------------------------------------------------- -------------Sleep ()

Call sleep in the thread T1 (time), T1 keep the lock object, into the blocking state, time completes, T1 into the executable state. gives the opportunity for low priority thread execution.

package thread;

public class Sleeprunnable implements Runnable {
	
	@Override public
	synchronized void  Run () {for
		 int i=0 ; i<3; i++) { 
             System.out.println (Thread.CurrentThread (). GetName () + ":" +i); 
             try{ 
                 Thread.Sleep (MB);	The thread called Sleep (100) and the other thread was still unable to enter the run function
             } 
             catch (Interruptedexception e) { 
                 System.out.println ("interrupted");

	}} public static void Main (string[] args) {
		sleeprunnable myrun = new sleeprunnable ();
		thread T1 = new Thread (myrun);
		T1.setname ("T1");
		Thread t2 = new Thread (myrun);
		T2.setname ("T2");
		T1.start ();
		T2.start ();
	}
	

Run Result:

t1:0
t1:1
t1:2
t2:0
t2:1
t2:2


--------------------------------------------------------------------------------------------------------------- -------------
yield ()

When the thread calls yield (), the thread goes directly into the executable state without putting the lock

package thread;

public class Yieldrunnable implements Runnable {
	
	@Override public
	void  Run () {//here if you use the synchronized tag, The other thread cannot enter for
		 (int i=0; i<3; i++) { 
             System.out.println (Thread.CurrentThread (). GetName () + ":" +i); 
             Thread.yield (); <span style= "White-space:pre" >		</span>//////////////
         } 
	}

	Public static void Main (string[] args) {
		yieldrunnable myrun = new yieldrunnable ();
		thread T1 = new Thread (myrun);
		T1.setname ("T1");
		Thread t2 = new Thread (myrun);
		T2.setname ("T2");
		T1.start ();
		T2.start ();
	}
	
Run Result:

t1:0
t2:0
t1:1
t2:1
t1:2
t2:2



--------------------------------------------------------------------------------------------------------------- -------------
Wait ()

When T1 is now calling wait (), the thread frees the owning lock and goes into the thread waiting pool. When other threads call notify, the JVM takes a thread from the threads wait pool and puts it in the lock flag waiting pool.

Package thread;
	
	public class Waitrunnable implements Runnable {Thread T1, T2;
		Public waitrunnable () {t1 = new Thread (this);
		T1.setname ("T1");
		t2 = new Thread (this);
	T2.setname ("T2"); @Override public synchronized void run () {if (Thread.CurrentThread () ==t1) {System.out.pri
					Ntln ("----------" +thread.currentthread (). GetName () + "Wait 10");  try {wait (); <span style= "White-space:pre" > </span>////////////} catch (Interruptedexception e)
					{E.printstacktrace (); for (int i = 0; i < i++) {System.out.println (thread.currentthread). GE 
			Tname ());
            	 } if (Thread.CurrentThread () ==t2) {System.out.println ("----------T2 done"); Notifyall (); <span style= "White-space:pre" > </span>////////////}else{SYSTEM.OUT.P
             Rintln ("----------T1 done"); }} public static void Main (StRing[] args {waitrunnable runnable = new waitrunnable ();
		Runnable.t1.start ();
	Runnable.t2.start (); }
	
}

Run Result:

----------T1 wait
T2 T2 T2 T2 T2 T2 T2 T2 T2 T2
----------T2 do
T1
t1
t1
t1
t1
t1
T1
t1 t1 t1
----------T1 done


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.