There is one such topic, using multi-threaded way to achieve the producer consumer model, the producer randomly produces a number between 0-1000, the consumer prints out the production data. When the randomly generated number is 0 o'clock, both the production line and the consumer thread exit.
Idea: Using a queue to store the generated data, queues as shared data are shared among producers and consumers.
Producers:
/**
* Data Producers
*/
Import Java.util.Queue;
Import Java.util.Random;
/**
* Name: Chinese name of Class <br>
* Function: Description of the function of the class <br/>
* <br/>
*
* @since JDK 1.7
* @see
* @author RDF
*/
public class Producernum implements Runnable {
Private Queue Q;
Public Producernum (Queue q) {
THIS.Q = q;
}
@Override
public void Run () {
Random random = new random ();
int count = 0;
while (true) {
int i = Random.nextint (10);
if (i! = 0) {
count++;
Q.add (i);
System.out.println (i);
try {
Thread.Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
else {
Q.add (i);
System.out.println ("This is the first" + Count + "times" + ":" + i);
Break
}
}
}
}
Consumers:
/**
* Consumer Data
*/
Package Pcmodl;
Import Java.util.Queue;
/**
* Name: Chinese name of Class <br>
* Function: Description of the function of the class <br/>
* <br/>
*
* @since JDK 1.7
* @see
* @author RDF
*/
public class Consumernom implements Runnable {
Private Queue Q;
/**
* Construction method: Consumernom.
*
*/
Public Consumernom (Queue q) {
THIS.Q = q;
}
@Override
public void Run () {
while (true) {
if (!q.isempty ()) {
if ((int) q.peek () = = 0) {
System.out.println (Q.peek ());
Break
}
System.out.println (Q.poll ());
System.out.println ("************");
try {
Thread.Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
else {
}
}
}
}
Main thread:
/**
* Main thread
*/
Package Pcmodl;
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
/**
* Name: Chinese name of Class <br>
* Function: Description of the function of the class <br/>
* <br/>
*
* @since JDK 1.7
* @see
* @author RDF
*/
public class Produceconsumer {
/**
* <b> name </b>: The Chinese name of the method <br/>
* <BR/>
* <b> description </b>: Description of the function and use points of the method <br/>
* <BR/>
*
* @param args
*
* @see
*/
public static void Main (string[] args) {
Queue q = new linkedlist<> ();
Executorservice service = Executors.newcachedthreadpool ();//use Newcachedthreadpool thread pool
Producernum p = new Producernum (q);
Consumernom C = new Consumernom (q);
thread T1 = new Thread (p);
Thread t2 = new Thread (c);
T1.start ();
T2.start ();
Service.submit (P);
Service.submit (c);
}
}
Summary: In the execution of the program, the consumer thread is always end without reason, not in execution, because I am not deep enough to learn multithreading, do not understand why this situation occurs. Personal speculation is that data sharing should be caused by thread blocking.
public class Consumernom implements Runnable {
Private Queue Q;
/**
* Construction method: Consumernom.
*
*/
Public Consumernom (Queue q) {
THIS.Q = q;
}
@Override
public void Run () {
while (true) {
if (!q.isempty ()) {
if ((int) q.peek () = = 0) {
System.out.println (Q.peek ());
Break
}
System.out.println (Q.poll ());
System.out.println ("************");
try {
Thread.Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
else {//Consumers add such a piece of code and go through the test without having the above problem.
try {
Thread.Sleep (500);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
Thread classes can also integrate the thread class:
Producers:
public class Producernum extends Thread {
Private Queue Q;
Public Producernum (Queue q) {
THIS.Q = q;
}
@Override
public void Run () {
Random random = new random ();
int count = 0;
while (true) {
int i = Random.nextint (10);
if (i! = 0) {
count++;
Q.add (i);
System.out.println (i);
try {
Thread.Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
else {
Q.add (i);
System.out.println ("This is the first" + Count + "times" + ":" + i);
Break
}
}
}
}
Consumers:
public class Consumernom extends Thread {
Private Queue Q;
/**
* Construction method: Consumernom.
*
*/
Public Consumernom (Queue q) {
THIS.Q = q;
}
@Override
public void Run () {
while (true) {
if (!q.isempty ()) {
if ((int) q.peek () = = 0) {
System.out.println (Q.peek ());
Break
}
System.out.println (Q.poll ());
System.out.println ("************");
try {
Thread.Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
else {
try {
Thread.Sleep (500);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
}
A simple example of multi-threading production consumption pattern