The previous synchronization lock to synchronize the behavior of the task, two tasks in the alternate access to shared resources, you can use a synchronization lock so that at any time only one task can access the resource, see blog: Thread synchronization of the Synchronized keyword. The following is mainly about how to make the tasks work together, so that multiple tasks can be worked with to solve the problem of wood, because some of the problems, some of the parts must be resolved before the other parts are solved, like in the restaurant waiter serve something dishes must have a chef cooked dishes. When a task is collaborating, you can suspend the task itself until some external conditions change, indicating that it is time to push the task forward.
Wait/notify
wait method suspends the task while waiting for changes in the outside world, and only when the Notify or Notifyall when it occurs, something of interest occurs, and the task is awakened and checked for changes.
The wait method indicates that the synchronization lock is actively released and the task is suspended, and the current task is in the waiting state. Because a lock is released, it means that other synchronized methods of the object can be called during wait. And just in these methods there are changes that are interested in waking the suspended task.
when calling wait method, it is declared: "I have just finished all the things I can do, so I will wait here, but I hope the other synchronized The operation notifies me when the condition is appropriate for me to proceed. "
The Notify method is used to wake up a task in the wait state, and when Notify/notifyall is called for a particular lock, only the task that waits for the lock is awakened.
Note:
Wait/notify/notifyall These methods are part of the base class object. Because any object can be used as a synchronous lock. And only in the synchronous control method or synchronous code block can call these methods, if the asynchronous method inside the call, the program can be compiled, run the error illegalmonitorstateexception. This means that calling these methods must have an object lock (synchronous lock).
Producer Consumers
In a restaurant, there is a chef and a waiter, the waiter must wait for the chef to prepare the meal, when the chef is ready, he will inform the waiter, then the waiter to serve, then return to continue to wait. This is an example of a task collaboration. The chef represents the producer, while the waiter represents the consumer.
Class Restaurant{private Object obj;public void put () {while (true) {synchronized (this) {a while (obj! = null) {try {wait ()}; } catch (Interruptedexception e) {e.printstacktrace ();}} obj = new Object (); System.out.print ("Order up! "); Notifyall ();}}} public void Get () {while (true) {synchronized (the This) {(obj = = null) {try {wait ();} catch (Interruptedexception e) {e. Printstacktrace ();}} System.out.println ("Waiter get Meal"); obj = Null;notifyall ();}}} Class Chef extends Thread{private Restaurant restaurant;public Chef (Restaurant Restaurant) {this.restaurant = Restaurant ;} @Overridepublic void Run () {restaurant.put ();}} Class Waiter extends Thread{private Restaurant restaurant;public waiter (Restaurant Restaurant) {this.restaurant = Restaurant;} @Overridepublic void Run () {restaurant.get ();}} public class Test {public static void main (string[] args) {Restaurant Restaurant = new Restaurant (); Chef produce = new Chef (restaurant); Waiter consumer = new Waiter (restaurant);p Roduce.start (); consumer. Start ();}} Output:
Order up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get Mealorder up! Waiter Get meal ...
Chef and waiter work through Resaurant, and in two threads, a thread is the chef thread that waits for the waiter to consume a good meal, and the thing he is interested in is whether obj is null or not. Once obj is null and the waiter consumes the meal, it wakes the waiter to consume the meal.
Another thread is the waiter thread hangs waiting for the cook to do the meal, and the thing he is interested in is also whether obj is null. Once obj is not NULL, he consumes the meal and wakes the chef to make the next meal.
In general, wait is wrapped in a while () statement, which constantly tests what is waiting. Can I change to if? Initially it appears that, once awakened, this obj must be acquired at all times, because the task is awakened because it is something of interest, but the thing is happening in concurrent applications because of the problem. Other tasks are likely to suddenly get in the wake of the current task and take out obj. Because the usual use of the following wait
while (Conditiopnisnotmet)
Wait ();
This is more secure and ensures that the conditions are met before exiting the loop.
Java concurrent Programming 6_ thread collaboration/producer-Consumer