java multithreaded pipeline method, semaphore method
1. Tube pass method:
Producer: the module responsible for production data (may be method, object, thread, process)
Consumer: module responsible for processing data (may be method, object, thread, process)
Buffer zone: consumers cannot directly use the producer's data. There is a buffer zone between them. The producer puts the produced data into the buffer zone, and consumers take out the data from the buffer zone
Idea:
1. First of all, there is a producer. Consumers and producers only care about production and consumers only care about consumption
2. A buffer is used to buffer an array of 10 sizes
3. There is a method called putting in the product. When the product is thrown in, we judge whether the buffer is full. If it is full, the producer will have to wait,
If it is not full, put the product in. If there is a product after putting it in, quickly inform consumers to consume
4. Consumers can judge whether they can consume. If there is something, I can consume it directly. After consumption, I will inform the producer of production.
If there is nothing, consumers will wait. Wait for the producer to notify him. After the producer notifies him, he can release the wait. Therefore, there are our wait() and notify() in this area.
Here, both wait() and notify() are provided by Object:
wait() causes the current thread to wait until another thread calls the notify() or notifyAll() methods of the object.
notify() wakes up a single thread waiting for the object monitor.
Once wait() is added, it also enters the blocking state, but this blocking is different from the previous sleep(). It will release the lock. The thread waiting () can hand over the lock to other threads for use, otherwise the resource will always be held by it.
When you have the idea, start the code part:
(1) Create producer
// producer class Productor extends Thread{ SynContainer container; public Productor(SynContainer container){ this.container = container; } // production @Override public void run() { for (int i = 0; i < 100; i++) { container.push(new Bug(i)); System.out.println("Produced"+i+"individual bug"); } } }
(2) Create destroyer
// bug Buster class Consumer extends Thread{ SynContainer container; public Consumer(SynContainer container){ this.container = container; } @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("Destroyed-->"+container.pop().id+"individual bug"); } } }
(3) Create bug area (product area)
// bug products class Bug{ int id;// bug number public Bug(int id){ this.id = id; } }
(4) Create synchronous code block (buffer)
// buffer class SynContainer{ // A container size is required Bug[] bugs = new Bug[10]; // Container calculator int count = 0; // Producer put bug public synchronized void push(Bug bug){ // If the container is full, you need to wait for the bug to be eliminated if(count==bugs.length){ // Notify the bug destroyer to eliminate the bug and wait for the production bug try { //Causes the current thread to wait until another thread calls the notify() method or notifyAll() method of the object. this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // If not, we need to continue to produce bug s bugs[count] = bug; count++; // You can inform the destroyer that it has been destroyed this.notifyAll(); // notifyAll wakes up all threads waiting for the object monitor. } // Exterminators eliminate bug s public synchronized Bug pop(){ // Judge whether it can be eliminated if (count == 0){ // Wait for producers to produce, exterminators to wait try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // If it can be eliminated count--; Bug bug = bugs[count]; // Notify the producer of production after elimination return bug; } }
Ensure that products are produced when there is no product in the container and cannot be consumed; When the product in the container is full, you can no longer produce the product
(5) Create test class
public class Test { public static void main(String[] args) { SynContainer container = new SynContainer(); new Productor(container).start(); new Consumer(container).start(); } }
(6) Testing
Perfectly eliminated all bug s
2. Signal lamp method:
Let's first understand what the semaphore method means: judge a flag. If it is true, let him wait. If it is false, let him notify another person and connect them. Just like our semaphore, the red light stops and the green light goes. Through such a judgment method, just judge what Rui wants to wait and wake him up.
Don't say much, just go to the code^^
package com.macro.mall.bo; //Test producer consumer problem 2: signal lamp method, solved by flag bit public class Test { public static void main(String[] args) { TV tv = new TV(); new Player(tv).start(); new Watcher(tv).start(); } } //Producer -- > actor 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) { this.tv.play("What is a happy planet"); } else { this.tv.play("If you want to know what a happy planet is, I'll take you to study it"); } } } } //Consumer -- > audience 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(); } } } //Products -- > Programs class TV { //The actor performs and the audience waits for T //The audience watched and the actors waited for F String voice; // A performance boolean flag = true; //perform public synchronized void play(String voice) { if (!flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("The actors performed:" + voice); //Inform the audience to watch this.notifyAll(); this.voice = voice; this.flag = !this.flag; } //watch public synchronized void watch() { if (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Watched:" + voice); //Inform the actors to perform this.notifyAll(); this.flag = !this.flag; } }
The first article, thank you for your kindness