Application scenarios

  • Application scenario: Producer and consumer issues
  • This is a thread synchronization problem, producers and consumers share the same resource, and producers and consumers depend on each other and condition each other.
  • For producers, consumers should be informed to wait before producing the product. After the product is produced, consumers need to be informed immediately.
  • For consumers, after consumption, producers should be notified that they have finished consumption and need to produce new products for consumption.
  • Synchronized is not enough in the producer consumption problem
    1. Synchronized prevents concurrent updates of a shared resource and implements synchronization
    2. Synchronized cannot be used for messaging between different threads.

Thread communication

  • Java provides several ways to solve the problem of communication between threads

    • Wait () : A thread waits until notified by another thread and, unlike sleep(), releases the lock.
    • Wait (long timeout) : Specifies the number of milliseconds to wait
    • Notify () : Wakes up a thread in the wait state
    • NotifyAll () : Wakes up all threads that call wait() on the same object. NotifyAll () : Wakes up all threads that call wait() on the same object.
  • Note: all is the method of Object class, can only use in synchronized methods or synchronized code block, or throws an exception lllegalMonitorStateException

  • Producer – consumer problems – pipeline approach

package com.Thread.lock; Public class TestPC {public static void main(String[] args) {Buffer Buffer = new Buffer(); Productor productor1 = new Productor(buffer, "P1"); Productor productor2 = new Productor(buffer, "P2"); Productor productor3 = new Productor(buffer, "P3"); Consumer consumer = new Consumer(buffer,"C1"); productor1.start(); productor2.start(); productor3.start(); consumer.start(); } // Productor extends Thread{private Buffer extends Thread; public Productor(Buffer buffer,String name){ super(name); this.buffer = buffer; } @Override public void run() { for (int i = 0; i < 10; i++) { Chicken chicken = new Chicken(i); System.out.println(thread.currentThread ().getName() + "); try { buffer.push(chicken); } catch (InterruptedException e) { e.printStackTrace(); }}}} // class Consumer extends Thread{private Buffer; public Consumer(Buffer buffer,String name){ super(name); this.buffer = buffer; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Chicken chicken = buffer.pop(); System.out.println(thread.currentThread ().getName() + "; } catch (InterruptedException e) { e.printStackTrace(); }}}} // product class Chicken{private int id; public Chicken(int id) { this.id = id; } public int getId() { return id; }} // Buffer class Buffer{Chicken[] Chickens = new Chicken[10]; int count = 0; Public synchronized void push(Chicken Chicken) throws InterruptedException { If (count == chickens. Length){// Notify consumers to spend, the producers wait this.wait(); } // If the container is not full, continue producing chickens[count] = chicken; count ++; // Notify the consumer to consume this.notifyall (); Public synchronized Chicken pop() throws InterruptedException {if (count == 0){ This. Wait (); } // count --; Chicken chicken = chickens[count]; // Notify the producer to produce this.notifyall (); return chicken; }}Copy the code
  • Producer-consumer Problem – Semaphore method (set marker)
package com.Thread.lock; Public class TestPC2 {public static void main(String[] args) {TV TV = new TV(); new Player(tv).start(); new Watcher(tv).start(); }} class Player extends Thread{TV TV; public Player(TV tv){ this.tv = tv; } @Override public void run() { for (int i = 0; i < 20; I ++) {if (I % 2 == 0){tv.play(" java.exe "); }else {tv.play(" tik Yin - record the beautiful life of rich people "); }}}} // Consumer class Watcher extends Thread{TV TV; public Watcher(TV tv){ this.tv = tv; } @Override public void run() { for (int i = 0; i < 20; i++) { tv.watch(); }} // Product: program class TV{String voice; boolean flag = true; public synchronized void play(String voice){ if (! Flag){try {// actors wait this.wait(); } catch (InterruptedException e) { e.printStackTrace(); }} system.out.println (" "+ voice); // Notify the viewer to watch this.notifyall (); this.voice = voice; this.flag = ! this.flag; } public synchronized void watch(){if (flag){try {// } catch (InterruptedException e) { e.printStackTrace(); }} system.out.println (" + this.voice "); // Notify the actor to act this.notifyall (); this.flag = ! this.flag; }}Copy the code