Java await wait sleep yield

Source: Internet
Author: User

Java Concurrency: await/wait with the Sleep , yield the difference between

whether to release the lock: called Sleep and the yield does not release the lock acquired by the current thread, but calls the await/wait , it releases the lock it acquires and blocks the wait.

When to restore after a call:

# Sleep let the thread block, and will not execute within the specified time, the time is back to the ready state, not necessarily be immediately scheduled to execute;

# yield just let the current object return to the ready state, or it is possible to be called again immediately execution.

# await/wait , it blocks above the conditional queue, and then a thread calls the corresponding notify/signal method will make the await/wait the thread back to the ready state is not necessarily immediately executed.

who's method: yield and the Sleep methods are Thread class, and wait method is Object of the class, await method is Condition Displays the condition queue.

Execution Environment: yield and the Sleep method can be placed anywhere in the thread, while the await/wait method must be placed inside the synchronization block, otherwise a run-time exception will be generated.


Await/wait

Sleep

Yield

Whether to release the held lock

Release

Do not release

Do not release

When to recover after a call

Enter ready state after wake up

After the specified time

Immediately into the ready state

Who's The way

Condition/object

Thread

Thread

Execution Environment

Synchronizing code blocks

Any position

Any position

Experiment

/**

*

*/

Package com.b510.test;

/**

The difference between sleep () and Wait () in *java

* @author Hongten

* @date 2013-12-10

*/

public class TestD {

public static void Main (string[] args) {

New Thread (New Thread1 ()). Start ();

try {

Thread.Sleep (5000);

} catch (Exception e) {

E.printstacktrace ();

}

New Thread (New Thread2 ()). Start ();

}

private static class Thread1 implements runnable{

@Override

public void Run () {

Synchronized (Testd.class) {

System.out.println ("Enter thread1 ...");

System.out.println ("Thread1 is waiting ...");

try {

calling the Wait () method, the thread discards the object lock and enters the waiting lock pool waiting for this object

TestD.class.wait ();

} catch (Exception e) {

E.printstacktrace ();

}

System.out.println ("Thread1 is going on ....");

System.out.println ("THREAD1 is over!!!");

}

}

}

private static class Thread2 implements runnable{

@Override

public void Run () {

Synchronized (Testd.class) {

System.out.println ("Enterthread2 ...");

System.out.println ("Thread2 is sleep ....");

Only after the Notify () method is called for this object does the thread enter the object lock pool to get the object lock into the running state.

TestD.class.notify ();

//==================

difference

If we put the code:TestD.class.notify (); to comment out, i.e. Testd.class called the Wait () method, but did not call notify ()

method, the thread is always in a pending state.

try {

The Sleep () method causes the program to pause execution for the specified time, yielding the CPU to that other thread,

But his monitoring status is still maintained, and when the specified time is up, it will automatically return to the running state.

During the call to the sleep () method, the thread does not release the object lock.

Thread.Sleep (5000);

} catch (Exception e) {

E.printstacktrace ();

}

System.out.println ("Thread2 isgoing on ....");

System.out.println ("THREAD2 is over!!!");

}

}

}

}

Operating effect:

Enter Thread1 ...

Thread1 is waiting ...

Enter Thread2 ....

Thread2 is sleep ....

Thread2 is going on ....

THREAD2 is OVER!!!

Thread1 is going on ....

THREAD1 is OVER!!!

If the code is commented out:

TestD.class.notify ();

Operating effect:

Enter Thread1 ...

Thread1 is waiting ...

Enter Thread2 ....

Thread2 is sleep ....

Thread2 is going on ....

THREAD2 is OVER!!!

And the program is always in a pending state.


Java await wait sleep 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.