Home > Back-end >  For Java producer consumer model, the error, how to solve??
For Java producer consumer model, the error, how to solve??

Time:02-25

Package com. BJSXT. Chapter17;

Import the Java. Util. Concurrent. ArrayBlockingQueue;
Import the Java. Util. Concurrent. TimeUnit;
Import the Java. Util. Concurrent. Atomic. AtomicInteger;

/* *
* @ author WMR
* @ date 2021/2/24
*/
Public class Main {
Public static void main (String [] args) {
//create the stored data blocking queue
ArrayBlockingQueue DrugQueue=new ArrayBlockingQueue (4);
//create a record number of atoms production integer
AtomicInteger drugCount=new AtomicInteger (0);
//create a record consumption data of atomic integer
AtomicInteger consumeCount=new AtomicInteger (0);
//create producers 2
Producer p1=new Producer (drugQueue drugCount);
P1. Elegantly-named setName (" producer - 1 ");
Producer p2=new Producer (drugQueue drugCount);
P2. Elegantly-named setName (" producer - 2 ");
//create consumer 3
Consumer c1=new Consumer (drugQueue consumeCount);
Consumer c2=new Consumer (drugQueue consumeCount);
Consumer c3=new Consumer (drugQueue consumeCount);
C1. Elegantly-named setName (" consumer - 1 ");
C2. Elegantly-named setName (" consumer - 2 ");
C3. Elegantly-named setName (" consumer - 3 ");
//start the producer thread, production data
P1. The start ();
P2. Start ();
//start consumer threads, consumption data
C1. Start ();
C2. Start ();
C3. The start ();
//every minute to check the blocking queue data quantity, production quantity, consumption amount
Try {
TimeUnit.SECONDS.sleep(1);
Int queueSize=drugQueue. The size ();
Int produceCount=drugCount. The get ();
Int consume=consumeCount. The get ();
System. Out.println (" queue have drugs: "+ queueSize +", the producer has produced: "+ produceCount +", "+
"Consumers are spending:" + consume);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}


Package com. BJSXT. Chapter17;

/* *
* @ author WMR
* @ date 2021/2/24
*/
Public class Drug {
//number
Private int id;
//price
Private int price;

Public int getId () {
Return the id;
}

Public int getPrice () {
The return price;
}

Public Drug (int id, int price) {
This. Id=id;
This. Price=price;
}
}


Package com. BJSXT. Chapter17;

Import the Java. Util. Concurrent. ArrayBlockingQueue;
Import the Java. Util. Concurrent. ThreadLocalRandom;
Import the Java. Util. Concurrent. Atomic. AtomicInteger;

/* *
* @ author WMR
* @ date 2021/2/24
*/
Public class Producer extends Thread {
//the number of production
Private AtomicInteger drugCount;
//save data blocking queue reference variables
Private ArrayBlockingQueue DrugQueue;
//reference variable assignment by constructing approach to
Public Producer (ArrayBlockingQueue DrugQueue, AtomicInteger drugCount) {
Enclosing drugQueue=drugQueue;
Enclosing drugCount=drugCount;
}
//production data
@ Override
Public void the run () {
While (true) {
Try {
Boolean add=drugQueue. Offer (new Drug (drugCount. IncrementAndGet (), ThreadLocalRandom. The current () nextInt (10)));
DrugCount. IncrementAndGet ();
NotifyAll ();
} the catch (Exception e) {
e.printStackTrace();
Try {
Wait ();
{} catch InterruptedException (ex)
ex.printStackTrace();
}
}
}
}
}


Package com. BJSXT. Chapter17;

Import the Java. Util. Concurrent. ArrayBlockingQueue;
Import the Java. Util. Concurrent. ThreadLocalRandom;
Import the Java. Util. Concurrent. TimeUnit;
Import the Java. Util. Concurrent. Atomic. AtomicInteger;

/* *
* @ author WMR
* @ date 2021/2/24
*/
Public class Consumer extends Thread {
The number of//consumption
Private AtomicInteger consumeCount;
//save data blocking queue reference variables
Private ArrayBlockingQueue DrugQueue;
//by constructing method for variable assignment
Public Consumer (ArrayBlockingQueue DrugQueue, AtomicInteger consumeCount) {
Enclosing drugQueue=drugQueue;
Enclosing consumeCount=consumeCount;
}
//keep consumption data
@ Override
Public void the run () {
While (true) {
Try {
Drug Drug=drugQueue. Poll ();
If (null==drug) {
Wait ();
}
UseDrug (drug. GetPrice ());
ConsumeCount. IncrementAndGet ();
NotifyAll ();
} the catch (Exception e) {
e.printStackTrace();
Try {
Wait ();
{} catch InterruptedException (ex)
ex.printStackTrace();
}
}

}
}
//use the
Public void useDrug (int price) {
Try {
//the price the more expensive to use the longer
TimeUnit. MILLISECONDS. Sleep (price);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}

Results:
"C: \ Program Files \ Java \ JDK - 11.0.8 \ bin \ Java. Exe" "- javaagent: D: \ software \ idea18 \ IntelliJ IDEA 2018.2.5 \ lib \ idea_rt jar=50566: D: \ software \ idea18 \ IntelliJ IDEA 2018.2.5 \ bin" - Dfile. Encoding=utf-8 - classpath D: \ project \ workspace \ u2vd_cloud, concurrency, 01 - Thread \ target \ classes com BJSXT. Chapter17. Main
Java. Lang. IllegalMonitorStateException
The at Java. The base/Java. Lang. Object. Wait (Native Method)
At Java. The base/Java. Lang. Object. Wait (328) Object. Java:
At com. BJSXT. Chapter17. Consumer. The run (29) Consumer. Java:
Java. Lang. IllegalMonitorStateException
The at Java. The base/Java. Lang. Object. NotifyAll (Native Method)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related