This blog is a previous Java Multithreading (iii) Producer consumer model and the implementation of the method complement. The producer consumer model is implemented in three methods (lock, synchronized, blocking queue). The specific content is: The producer produces the random number (in order to facilitate the reading result, I limit the random number to 10 integers), the consumer reads and prints. 1 blocking queues to realize producer consumer model
Blocking queues is the simplest way to implement
Import Java.util.concurrent.BlockingQueue;
Import Java.util.concurrent.LinkedBlockingQueue;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import Java.util.Random; public class Blockingqueuepattern {public static void main (String args[]) {//blocking queue Blockingqueue Sharedque
UE = new Linkedblockingqueue ();
Create producer threads and consumer threads thread Prodthread = new Producer (sharedqueue));
Thread consthread = new Thread (new Consumer (Sharedqueue));
Start producer thread and Consumer thread Prodthread.start ();
Consthread.start ();
}//Producer class Producer implements Runnable {private final blockingqueue sharedqueue;
Public Producer (Blockingqueue sharedqueue) {this.sharedqueue = Sharedqueue; @Override public void Run () {for (int i=0; i<10; i++) {try {//Generate 10 random
The integer is placed in the blocking queue Random Random = new Random ();
int Prodrandom=random.nextint (10); SystEm.out.println ("produced:" + Prodrandom);
Sharedqueue.put (Prodrandom); The catch (Interruptedexception ex) {Logger.getlogger (Producer.class.getName ()). log (Level.severe, NULL, ex)
;
"}}//Consumer class Consumer implements runnable{private final blockingqueue sharedqueue;
Public Consumer (Blockingqueue sharedqueue) {this.sharedqueue = Sharedqueue; @Override public void Run () {while (true) {try {System.out.println ("consum
Ed: "+ Sharedqueue.take ()); The catch (Interruptedexception ex) {Logger.getlogger (Consumer.class.getName ()). log (Level.severe, NULL, ex)
; Output:produced:4 produced:7 produced:8 consumed:4 consumed:7 produced:6 consumed:8 Consumed:
6 produced:1 produced:7 consumed:1 consumed:7 produced:3 produced:5 consumed:3 consumed:5 produced:9 produced:7 Consumed:9 ConsUmed:7
2 Lock realizes producer consumer model
Since we don't need Java to provide our out-of-the-box blocking queues, we might as well create a queue ourselves, as follows:
Import java.util.LinkedList;
Import Java.util.Random;
Import java.util.concurrent.locks.Condition;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock; public class Optimisticlockpattern {public static void main (string[] args) {selfqueue selfqueue = new S
Elfqueue ();
Create producer threads and consumer threads thread Prodthread = new Producer (selfqueue));
Thread consthread = new Thread (new Consumer (Selfqueue));
Start producer thread and Consumer thread Prodthread.start ();
Consthread.start ();
class selfqueue{int max = 5;
linkedlist<integer> prodline = new linkedlist<integer> ();
Lock lock = new Reentrantlock ();
Condition full = lock.newcondition ();
Condition empty = Lock.newcondition ();
public void produce (int prodrandom) {try {lock.lock ();
while (max = = Prodline.size ()) {System.out.println ("storage reaches the upper limit, please wait"); Full.await ();
} prodline.add (Prodrandom);
Empty.signal ();
catch (Interruptedexception e) {e.printstacktrace ();
}finally{Lock.unlock ();
The public int consume () {int m = 0;
try {lock.lock ();
while (prodline.size () = = 0) {System.out.println ("queue is empty, please wait");
Empty.await ();
} m = Prodline.removefirst ();
Full.signal ();
catch (Interruptedexception e) {e.printstacktrace ();
}finally{Lock.unlock ();
return m;
}}//Producer class Producer implements runnable{private final selfqueue selfqueue;
Public Producer (Selfqueue selfqueue) {this.selfqueue = Selfqueue;
public void Run () {for (int i = 0; i < i++) {Random Random = new Random (); int Prodrandom=random.Nextint (10);
System.out.println ("produced:" + Prodrandom);
Selfqueue.produce (Prodrandom);
}}//Consumer class Consumer implements runnable{private final selfqueue selfqueue;
Public Consumer (Selfqueue selfqueue) {this.selfqueue = Selfqueue;
public void Run () {while (true) {System.out.println ("consumed:" + selfqueue.consume ());
"}}} output:produced:1 produced:9 consumed:1 consumed:9 queue is empty, please wait for produced:9 produced:1 Consumed:9 Consumed:1 consumed:8 queue is empty, please wait produced:6 produced:8 consumed:6 produced:4 consumed:8 produced:4 consumed:4 Pro duced:0 consumed:4 consumed:0 queue is empty, please wait
3 synchronized realize producer consumer model
Synchronized does not need to unlock itself manually, where the Wait () ¬ify () method mentioned earlier is used.
Import Java.util.Random; public class Pessimisticlockpattern {public static void main (string[] args) {Selfqueue selfqueue = new
Selfqueue ();
Create producer threads and consumer threads thread Prodthread = new Producer (selfqueue));
Thread consthread = new Thread (new Consumer (Selfqueue));
Start producer thread and Consumer thread Prodthread.start ();
Consthread.start ();
Class selfqueue{int index = 0;
int[] Prodline = new Int[6];
public synchronized void Produce (int prodrandom) {while (index = = prodline.length) {try {
This.wait ();
catch (Interruptedexception e) {e.printstacktrace ();
} this.notify ();
Prodline[index] = Prodrandom;
index++;
public synchronized int consume () {while (index = = 0) {try {this.wait (); catch (Interruptedexception e) {E.priNtstacktrace ();
} this.notify ();
index--;
return Prodline[index];
}//Producer class Producer implements runnable{private final selfqueue selfqueue;
Public Producer (Selfqueue selfqueue) {this.selfqueue = Selfqueue;
public void Run () {for (int i = 0; i < i++) {Random Random = new Random ();
int prodrandom = Random.nextint (10);
System.out.println ("produced:" + Prodrandom);
Selfqueue.produce (Prodrandom);
}}//Consumer class Consumer implements runnable{private final selfqueue selfqueue;
Public Consumer (Selfqueue selfqueue) {this.selfqueue = Selfqueue;
public void Run () {while (true) {System.out.println ("consumed:" + selfqueue.consume ());
}} output:produced:3 produced:3 consumed:3 produced:8 produced:3 consumed:3 produced:2 produced:6 consumed:3
Produced:7 Produced:8 produced:1 Produced:9Consumed:6 consumed:9 consumed:1 consumed:8 consumed:7 consumed:2