Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Write a program to simulate producer-consumer problem using bounded buffer whose size is 10. (this means the buffer only can store 10 messages.) For

Question: Write a program to simulate producer-consumer problem using bounded buffer whose size is 10. (this means the buffer only can store 10 messages.) For this, there will be one program creating two threads (one for producer and the other for consumer) which act as different processes. After 15 seconds of running, the program (both producer and consumer threads) should be terminated gracefully without any error.

I have this code:

import java.security.SecureRandom; import java.util.concurrent.*; /** * Created by Leon.H on 2016/1/13. */ public class ProducerConsumer { private int producerNumber = 0; private int consumerNumber = 0; private int bufferSize = 0; private final int seconds; public ProducerConsumer(int producerNumber, int consumerNumber, int bufferSize, int seconds) { this.producerNumber = producerNumber; this.consumerNumber = consumerNumber; this.bufferSize = bufferSize; this.seconds = seconds; System.out.println(this.producerNumber+ ": the number of producer threads"); System.out.println(this.consumerNumber+ ": the number of consumer threads"); System.out.println(this.bufferSize+ ": the number of producer threads"); } public void process() throws InterruptedException { ExecutorService producerExecutorService = Executors.newFixedThreadPool(this.producerNumber); ExecutorService consumerExecutorService = Executors.newFixedThreadPool(this.consumerNumber); BlockingQueue integers = new ArrayBlockingQueue(this.bufferSize); for (int i = 0; i  integers; public ProducerTask(BlockingQueue integers) { this.integers = integers; } public void run() { while (true) { Integer content = new SecureRandom().nextInt(1000); System.out.println("Producer #" + Thread.currentThread().getId() + " put: " + content); integers.offer(content); try { Thread.sleep(new SecureRandom().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class ConsumerTask implements Runnable { private final BlockingQueue integers; public ConsumerTask(BlockingQueue integers) { this.integers = integers; } public void run() { while (true) { try { System.out.println("Consumer #" + Thread.currentThread().getId() + " get: " + integers.take()); } catch (InterruptedException e) { e.printStackTrace(); } try { Thread.sleep(new SecureRandom().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
import org.testng.annotations.Test; /** * Created by Leon.H on 2016/1/13. */ public class ProducerConsumerTest { @Test public void oneProducerOneConsumerSizeOne() throws InterruptedException { int ProducerNumber = 15; int ConsumerNumber = 15; int size = 1; int seconds=5; ProducerConsumer producerConsumer = new ProducerConsumer(ProducerNumber, ConsumerNumber, size, seconds); producerConsumer.process(); }
}
image text in transcribed
How to start integer from 0 to 14 and message from 1 to 10?
15: the number of producer threads 15: the number of consumer threads 1: the number of producer threads Producer #30 put: 844 Producer #34 put: 138 Producer #24 put: 824 Producer #32 put: 166 Producer #27 put: 908 Producer #37 put: 673 Producer #28 put: 95 Producer #25 put: 292

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

Who are the users of managerial accounting information?

Answered: 1 week ago