Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

User import java.util.ArrayList; import java.util.List; class Producer implements Runnable { private final List buffer; public Producer ( List buffer ) { this.buffer = buffer; }

User
import java.util.ArrayList; import
java.util.List;
class Producer implements Runnable {
private final List buffer;
public Producer(List buffer){
this.buffer = buffer;
}
@Override
public void run(){
String names[]={" first-dose", "seconed-dose", "booster-dose"};
for (String name : names){
synchronized (buffer){
buffer.add(name);
try {
Thread.sleep(500);
} catch (InterruptedException e){
}
}
System.out.println(Thread.currentThread().getName()+" add to list "+ name);
}
System.out.println(Thread.currentThread().getName()+" exit "+ Main.finish);
synchronized (buffer){ buffer.add(Main.finish);
}
}
}
class Consumer implements Runnable {
private final List buffer; public
Consumer(List buffer){
this.buffer = buffer;
}
@Override
public void run(){ while
(true){ synchronized
(buffer){
if (buffer.isEmpty()){
continue;
}
if (buffer.get(0).equals(Main.finish)){
System.out.println(Thread.currentThread().getName()+" exiting.");
break;
} else {
System.out.println(Thread.currentThread().getName()+" remove from list "+
buffer.remove(0));
System.out.println("------------------------------------------------");
try {// create thread for consumer
Thread.sleep(1000);//set the delay for one seconed
} catch (InterruptedException e){
}
}
}
}
}
}
public class Main {
public static final String finish= "finish"; // end the process
public static void main(String[] args){
List buffer = new ArrayList<>(); // buffer we used Thread
Thread producerThread1= new Thread(new Producer(buffer));
producerThread1.setName("producer Thread 1");
Thread producerThread2= new Thread(new Producer(buffer));
producerThread2.setName("producer Thread 2");
Thread consumerThread1= new Thread(new Consumer(buffer));
consumerThread1.setName("consumer Thread 1");
Thread consumerThread2= new Thread(new Consumer(buffer));
consumerThread2.setName("consumer Thread 2");
producerThread1.start();
producerThread2.start();
consumerThread1.start();
consumerThread2.start(); }
this is the output
producer Thread 1 add to list first-dose
consumer Thread 2 remove from list first-dose
------------------------------------------------
producer Thread 2 add to list first-dose
consumer Thread 1 remove from list first-dose
------------------------------------------------
producer Thread 1 add to list seconed-dose
consumer Thread 1 remove from list seconed-dose
------------------------------------------------
producer Thread 2 add to list seconed-dose
consumer Thread 1 remove from list seconed-dose
------------------------------------------------
producer Thread 1 add to list booster-dose
consumer Thread 2 remove from list booster-dose
------------------------------------------------
producer Thread 1 exit finish
producer Thread 2 add to list booster-dose
producer Thread 2 exit finish
consumer Thread 1 remove from list booster-dose
------------------------------------------------
consumer Thread 1 exiting.
consumer Thread 2 exiting. ineed same this output rewrite this code i need same output

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

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions