Thread waits and notifications

Source: Internet
Author: User
Tags static class

public class wait and notify {
Final static Object Ob=new object ();
public static class T1 extends thread{
@Override
public void Run () {
Synchronized (OB) {
System.out.println ("T1-----------I am now awaiting execution");
try {
Thread.Sleep (2000);
Ob.wait ();
System.out.println ("T1-----------I'm going to start");
Thread.Sleep (3000);
System.out.println ("T1-----------My execution ended, Wake T2");
Ob.notify ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}

}
}
}
public static class T2 extends thread{
@Override
public void Run () {
Synchronized (OB) {
System.out.println ("T2-----------I do Now");
try {
Thread.Sleep (1000);
System.out.println ("T2-----------I release the Executive Right");
Ob.notify ();
System.out.println ("T2-----------I get the executive right again, start executing");
Thread.Sleep (5000);
System.out.println ("T2-----------I wait for the execution of the T1");
Ob.wait ();
System.out.println ("T2-----------I have completely finished");
} catch (Exception e) {
E.printstacktrace ();
}

}
}
}

public static void Main (string[] args) {
Thread t1=new T1 ();
Thread t2=new T2 ();
T1.start ();
T2.start ();
/* Perform steps
T1 executes first, then blocks this thread, waits for wake
T2 gets the OB to start executing, wakes up one of the waiting threads, and other threads must wait for it to finish before executing
T1 again get executive right to execute
* */
/*wait ():

Wait for the object's synchronization lock, need to obtain the object's synchronization lock to call this method, otherwise the compilation can pass, but the runtime will receive an exception: Illegalmonitorstateexception.

Calling the Wait () method of an arbitrary object causes the thread to block, the thread cannot continue execution, and the lock on the object is freed.

Notify ():

Wake up the thread waiting for the object to synchronize the lock (wake only one, if there are multiple waits), note that when this method is called, it does not wake up the thread of a waiting state exactly, but is determined by the JVM to wake up which thread, and not by priority.

Calling the Notify () method of an arbitrary object causes a randomly selected unblocking in a thread that is blocked by calling the wait () method of the object (but is not really executable until the lock is acquired).

Notifyall ():

Wake up all waiting threads, notice that the thread that wakes up is notify before wait, and has no effect on the wait thread after notify. */
}
}
Transfer from http://www.cnblogs.com/anxbb/p/8425326.html

Thread waits and notifications

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.