First, thread communication (producer consumer issues):
1, Thread communication: a thread to complete its own task, to notify another thread to complete another task.
2, the Classic question : producer and consumer issues.
1 There is a thread safety problem: There may be a price disorder, so to lock, and here the product P object is unique, can be used as a lock.
Package sram.thread; Product class product{String name; Name double price; Price}//Producer class Producer extends thread{Product p;
Products Public Producer (product p) {THIS.P = P;
public void Run () {int i = 0;
while (true) {synchronized (p) {if (i%2==0) {p.name = "Apple";
P.price = 6;
}else{p.name = "banana";
P.price = 3;
} i++;
System.out.println ("Production of" +p.name+ ", the unit price of" +p.price+ ");
}}//Consumer class customer extends thread{Product p;
Public Customer (Product p) {this.p = P; public void Run () {while (true) {synchronized (p) {System.out.println ("consumed" +p.name
+ "Unit Price" +p.price+ "Yuan");
}}} public class Proandcust {public static void main (string[] args) {Product P = new product ();
Create production object Producer Producer = new Producer (p);
Create consumer Customer customer = new Customer (p);
Invoke the Start method to open the thread Producer.start ();
Customer.start (); }
}
2 If I want to do to generate one, consume one output form how to do it?
So here we introduce the wait () and notify () method in the thread.
Second, wait () and notify () method detailed
1, improved version of producer consumer issues:
Package sram.thread; Product class product{String name; Name double price; Price Boolean flag = false;
The identification of whether the product is finished or not, the default is no production complete. }//Producer class Producer extends thread{Product p;
Products Public Producer (product p) {THIS.P = P;
public void Run () {int i = 0;
while (true) {synchronized (p) {if (P.flag==false) {if (i%2==0) {
P.name = "Apple";
P.price = 6;
}else{p.name = "banana";
P.price = 3;
} i++;
System.out.println ("Production of" +p.name+ ", the unit price of" +p.price+ ");
P.flag = true;
P.notify ()//wake up consumers to consume}else{//have finished production, waiting for consumers to consume try {
Producer waits for P.wait (); catch (InterruptedexCeption e) {e.printstacktrace ();
}}}}//Consumer class customer extends thread{Product p;
Public Customer (Product p) {this.p = P;
public void Run () {while (true) {synchronized (p) {if (p.flag==true) {//product has been produced
System.out.println ("Consumption of" +p.name+ "unit Price" +p.price+ "Yuan");
P.flag = false;
P.notify ()//wake producers to produce}else{//products have not yet been produced, should wait for producers to produce first.
try {p.wait ();//Consumer Wait} catch (Interruptedexception e) {
E.printstacktrace ();
public class Proandcust {public static void main (string[] args) {
Product P = new product ();
Create production object Producer Producer = new Producer (p);
Create consumer Customer customer = new Customer (p);
Invoke the Start method to open the thread Producer.start ();
Customer.start (); }
}
2, the API detailed:
Wait (): Waits if the thread executes the wait method, the thread enters the waiting state, and the thread in the waiting state must be called by another thread to invoke the Notify method to wake.
Notify (): Wakes up the wake thread pool waiting for one of the threads.
Notifyall (): Wakes up the thread pool for all waiting threads.
3, wait and notify method should pay attention to matters:
1 The Wait method and the Notify method belong to object objects.
Analysis: These two methods are called by the lock object, and the object can be of any type.
2 The Wait method and the Notify method must be used in a synchronized code block or in a synchronization function.
Analysis: Locks are used only in synchronized code blocks and in synchronization functions.
3 The Wait method and the Notify method must be invoked by the lock object.
Analysis: See illustration.