Thread state diagram

 

 Wait wake Case: communication between threads

 

 Code

demo06 Package Penalty for; 
/ *
* wait wake Case: communication between threads
* Create a thread Customers: Consumers inform the number and types of buns boss want, call the wait method, gives up the CPU to enter WAITING state
* bedstead a boss thread : It took 5 seconds. Call nitify notify customers
*
* Note: Customer boss threads have to use synchronous code block package, to ensure that there is a wait and wake up only in the implementation
* lock synchronization objects used, must be guaranteed to be unique
* lock object can only call wait and notify methods
*
* * /
public class demo06 {
public static void main (String [] args) {
// create a lock object
Object obj = new new Object ();
// create a thread customers (consumers)
new new the thread ( ) {
@Override
public void RUN () {
// ensure thread waiting to wake up and only one execution, requires the use of synchronous technology
the synchronized (obj) {
System.out.println ( "kind of boss to inform the number of buns" );
// Call wait () method, to give cpu execution proceeds to WAITING state (wait indefinitely)
the try {
obj.wait ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
performed after wakeup // Code
System .out.println ( "bun've done");
}
}
} .start ();
// create a thread boss (producer)
new new the thread () {
@Override
public void RUN () {
// spend five seconds baking
the try {
the Thread.sleep (5000);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
// call nitify inform the customer of
the synchronized (obj) {
System.out.println ( "good boss after 5 seconds buns, inform the customer, can a bun");
obj.notify ();
}
}
} .start ();
}
}

Plus an infinite loop

demo06 Package Penalty for; 
/ *
* wait wake Case: communication between threads
* Create a thread Customers: Consumers inform the number and types of buns boss want, call the wait method, gives up the CPU to enter WAITING state
* bedstead a boss thread : It took 5 seconds. Call nitify notify customers
*
* Note: Customer boss threads have to use synchronous code block package, to ensure that there is a wait and wake up only in the implementation
* lock synchronization objects used, must be guaranteed to be unique
* lock object can only call wait and notify methods
*
* * /
public class demo06 {
public static void main (String [] args) {
// create a lock object
Object obj = new new Object ();
// create a thread customers (consumers)
new new the thread ( ) {
@Override
public void RUN () {
// has been waiting for
the while (to true) {
// wait and wake ensure only one thread execution, it requires the use of synchronous technology
synchronized (obj) {
System.out.println ( "the number of kinds of told the boss to the buns");
// call the wait () method, give up cpu executed, enter the WAITING state (wait indefinitely)
the try {
obj.wait ();
} the catch ( E InterruptedException) {
e.printStackTrace ();
}
// wake-up code execution after
System.out.println ( "bun've done");
}
}}
} .start ();
// create a thread boss (production person)
new new the Thread () {
@Override
public void RUN () {
the while (to true) {
// do spend five seconds buns
try {
Thread.sleep (5000);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
// call nitify inform the customer of
the synchronized (obj) {
System.out.println ( "good boss buns after five seconds, informing customers, can the bun ");
obj.notify ();
}
}}

} .start ();
}
}

Object class wait method to automatically wake time after

Wake-up method:

1void notify wake up a single thread

2void notifyAll wakes up all the threads waiting on the monitor again object

demo06 Package Penalty for; 
/ *
* wait wake Case: communication between threads
* Create a thread Customers: Consumers inform the number and types of buns boss want, call the wait method, gives up the CPU to enter WAITING state
* bedstead a boss thread : It took 5 seconds. Call nitify notify customers
*
* Note: Customer boss threads have to use synchronous code block package, to ensure that there is a wait and wake up only in the implementation
* lock synchronization objects used, must be guaranteed to be unique
* lock object can only call wait and notify methods
*
* * /
public class demo06 {
public static void main (String [] args) {
// create a lock object
Object obj = new new Object ();
// create a thread customers (consumers)
new new the thread ( ) {
@Override
public void RUN () {
// has been waiting for
the while (to true) {
// wait and wake ensure only one thread execution, it requires the use of synchronous technology
synchronized (obj) {
System.out.println ( "Customer 1 boss to inform the number of types of buns");
// call the wait () method, give up cpu executed, enter the WAITING state (wait indefinitely)
the try {
obj.wait (5000);
the catch} (InterruptedException E) {
e.printStackTrace ();
}
Code executed after wake //
System.out.println ( "bun has done");
}
}}
} .start ();
new new the Thread () {
@override
public void RUN () {
// has been waiting for
the while (to true) {
// wait and wake ensure only one thread execution, it requires the use of synchronous technology
the synchronized (obj) {
System.out.println ( "Number of customers 2 types of boss to inform the buns");
// call the wait () method, give up cpu executed, enter the WAITING state (wait indefinitely)
the try {
obj. the wait (5000);
} the catch (InterruptedException E) {
; e.printStackTrace ()
}
Code executed after wake //
System.out.println ( "bun has done");
}
}}
} .start ();
// create a thread boss (producer)
new new the thread () {
@Override
public void RUN () {
the while (to true) {
// spend five seconds baking
the try {
Thread.sleep (5000);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
// call nitify inform the customer of
the synchronized (obj) {
System.out.println ( "Boss 5 after the buns do the second, informing customers, can the bun ");
obj.notifyAll ();
}
}}

} .start ();
}
}

 

Guess you like

Origin www.cnblogs.com/Damocless/p/11875568.html