1, the necessity of thread communication
Multithreading not only shares resources, but also runs forward with each other.
2. Method of thread communication (all defined in object)
3 methods:
1) Wait () can run into a blocking state, put the lock
2) notify () block into the operational state, to obtain the lock
3) Notifyall () all threads that call the wait method and are suspended are restarted with the condition that wait and notifyall must belong to the same object
Must be used in a synchronous method or in a synchronous code block
3. Shared resource Class (warehouse)
Note: Share resources (products), contain information (product is not)
package com.ljb.app.communication;/** * shared resources Classes * @author ljb * @ VERSION 2015 Year March 11 */public class ShareData { // product private char c; // contain information (product has no, start without product in warehouse) private boolean isProduced = false; /* * Products (production products) */ public synchronized void putsharechar (char c) { // If the consumer has not yet consumed or has a product in the warehouse, the producer waits for if ( isproduced) { system.out.println ("Consumers are not yet consumed, so producers stop production.") "); try { wait ();// producer waits for } catch (interruptedexception e) { e.printstacktrace (); } } // warehouse in the production of products this.c = c; // Change signal volume (with products) isProduced = true; // Inform consumer consumption notify (); system.out.println ("producer produces products" + c + ", Inform consumer consumption ..."); } /* * Products (consumer products) */ public synchronized char getsharechar () { // If the producer is not in production or there is no product in the warehouse, the consumer waits for if (!isproduced) { system.out.println ("Producers are not yet produced, so consumers stop spending.") "); try { wait ();// consumer wait } catch (interruptedexception e) { e.printstacktrace (); } } // Change the semaphore (no product or consumption of the product) isProduced = false; // inform producers to produce notify (); system.out.println ("Consumers consume products" + c + ", inform producer of production ..."); return this.c; }}
4. Producer Threads
Package com.ljb.app.communication;/** * Producer threads, producing one product at a time * @author LJB * @version March 11, 2015 */public class Producer EXT Ends thread{//Shared resource object private Sharedata s; Method public Producer (Sharedata s) {THIS.S = s;} public void Run () {for (char ch = ' A '; Ch <= ' D '; ch++) {try {thread.sleep (int) (Math.random () *3000)); } catch (Interruptedexception e) {e.printstacktrace (); } s.putsharechar (ch);//Put the product into the warehouse}}}
5. Consumer Threads
Package com.ljb.app.communication;/** * Consumer thread * @author LJB * @version March 11, 2015 */public class Consumer extends thread{ Shared resources private Sharedata s; Construction method Public Consumer (Sharedata s) {THIS.S = s;} public void Run () {char ch; Do {try {thread.sleep (int) (Math.random () *3000)); } catch (Interruptedexception e) {e.printstacktrace (); } ch = S.getsharechar ();//Remove the product from the warehouse} while (ch! = ' D '); }}
6. Test class
Package com.ljb.app.communication;/** * Test class * @author LJB * @version March 11, 2015 */public class Test {/** * @param args */public static void main (string[] args) {sharedata s = new Sharedata (); Thread consumer = new consumer (s); Consumer.start (); Thread producer = new producer (s); Producer.start (); }}
Operation Result:
Producers are not yet produced, so consumers stop spending.
The producer produces a product, informs the consumer to consume ...
Consumers consume a product, notify producers of production ...
The producer produced the product B, informing the consumer of the consumption ...
The consumer consumes the product B, informs the producer to produce ...
The producer produces the product C, informs the consumer to consume ...
Consumers consume the product C, inform the producer of the production ...
The producer produces the product D, informs the consumer to consume ...
Consumers consume the product D, inform the producer of the production ...
Thread communication (producer and consumer issues)