Question
Explain Factory, Producer and Consumer codes on a report. And also answer the following questions in your report; 1. List the processes (threads) running when
Explain Factory, Producer and Consumer codes on a report. And also answer the following questions in your report; 1. List the processes (threads) running when you run the application 2. Which one of them are sharing information? 3. What kind of sharing mechanism is being used and how?
package Factory; import java.util.Date; import java.lang.*; import java.util.logging.Level; import java.util.logging.Logger;
public class Factory { public static void main(String args[]) { // creates the message queue Channel queue = new Channel(); // Creates the producer and consumer threads and pass Thread producer = new Thread(new Producer(queue)); Thread consumer = new Thread(new Consumer(queue)); // start the threads producer.start(); consumer.start(); } } class Channel{ // New Class added private Date date; public void send(Date message){ this.date =message; } public Date receive(){ return date; } } class Producer implements Runnable { private Channel queue; public Producer(Channel queue) { this.queue = queue; } public void run() { Date message; while (true) { try { // nap for awhile Thread.sleep(10); } catch (InterruptedException ex) { Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); } // produces an item and enter it into the buffer message = new Date(); System.out.println("Producer produced " + message); queue.send(message); } } } class Consumer implements Runnable{ private Channel queue; public Consumer(Channel queue) { this.queue = queue; } public void run() { Date message; while(true) { try { // nap for awhile Thread.sleep(10); } catch (InterruptedException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } message = queue.receive(); if (message != null) System.out.println("Consumer consumed " + message); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started