0% found this document useful (0 votes)
87 views

Producer Consumer Problem

This is Java implementation of the OS problem of process synchronization called The bounded buffer problem or the Producer-Consumer problem.

Uploaded by

SheenamBhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Producer Consumer Problem

This is Java implementation of the OS problem of process synchronization called The bounded buffer problem or the Producer-Consumer problem.

Uploaded by

SheenamBhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PCS {

public static void main(String args[]) {
Vector sared!ueue " ne# Vector();
int si$e " %;
&read prod&read " ne# &read(ne# Producer(sared!ueue' si$e)' (Producer();
&read cons&read " ne# &read(ne# Consumer(sared!ueue' si$e)' (Consumer();
S)stem.out.print((Commencing Solution*+n();
prod&read.start();
cons&read.start();
,
,

class Producer implements -unnable {

private .inal Vector sared!ueue;
private .inal int S/01;

public Producer(Vector sared!ueue' int si$e) {
tis.sared!ueue " sared!ueue;
tis.S/01 " si$e;
,

23verride
public void run() {
.or (int i " 4; i 5 %; i66) {
S)stem.out.println((Producer Produced /tem* ( 6 i);
tr) {
produce(i);
, catc (/nterrupted17ception e7) {
Logger.getLogger(Producer.class.get8ame()).log(Level.S1V1-1' null' e7);
,

,
,

private void produce(int i) tro#s /nterrupted17ception {

99#ait i. :ueue is .ull
#ile (sared!ueue.si$e() "" S/01) {
s)ncroni$ed (sared!ueue) {
S)stem.out.println((+n!ueue is .ull ( 6 &read.current&read().get8ame()
6 ( is #aiting ' si$e* ( 6 sared!ueue.si$e()6(+n();

sared!ueue.#ait();
,
,

99producing element and noti.) consumers
s)ncroni$ed (sared!ueue) {
sared!ueue.add(i);
sared!ueue.noti.);ll();
,
,
,

class Consumer implements -unnable {

private .inal Vector sared!ueue;
private .inal int S/01;

public Consumer(Vector sared!ueue' int si$e) {
tis.sared!ueue " sared!ueue;
tis.S/01 " si$e;
,

23verride
public void run() {
#ile (true) {
tr) {
S)stem.out.println((Consumer Consumed /tem* ( 6 consume());
&read.sleep(<4);
, catc (/nterrupted17ception e7) {
Logger.getLogger(Consumer.class.get8ame()).log(Level.S1V1-1' null' e7);
,

,
,

private int consume() tro#s /nterrupted17ception {
99#ait i. :ueue is empt)
#ile (sared!ueue.is1mpt)()) {
s)ncroni$ed (sared!ueue) {
S)stem.out.println((+n!ueue is empt) ( 6 &read.current&read().get8ame()
6 ( is #aiting ' si$e* ( 6 sared!ueue.si$e()6(+n();

sared!ueue.#ait();
,
,

993ter#ise consume element and noti.) #aiting producer
s)ncroni$ed (sared!ueue) {
sared!ueue.noti.);ll();
return (/nteger) sared!ueue.remove(4);
,
,
,

You might also like