You are on page 1of 4

OS LAB – 5

Name: Ivan Christopher

Reg.No: 20bci7097

CODE:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package os.lab.pkg4;

/**

* @author ivanc

*/

import java.util.*;

public class s {

public static class produce_consume {

LinkedList<Integer> list = new LinkedList<>();

int n = 3;

int v = 0;

public void produce() throws InterruptedException {

synchronized (this) {

while (list.size() == n) {

wait();

list.add(v++);
System.out.println(" Produce " + v + " buffer is " + list);

notify();

Thread.sleep(100);

public void consume() throws InterruptedException {

synchronized (this) {

while (list.size() == 0) {

wait();

int data = list.removeFirst();

System.out.println(" Consumed : " + data + " buffer is " + list);

notify();

Thread.sleep(100);

public static void main(String[] args)

throws InterruptedException {

final produce_consume o = new produce_consume();

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {


try {

o.produce();

} catch (InterruptedException e) {

});

Thread t2 = new Thread(new Runnable() {

@Override

public void run() {

try {

o.produce();

} catch (InterruptedException e) {

});

Thread t3 = new Thread(new Runnable() {

@Override

public void run() {

try {

o.consume();

} catch (InterruptedException e) {

});

Thread t4 = new Thread(new Runnable() {

@Override

public void run() {

try {
o.consume();

} catch (InterruptedException e) {

});

t1.start();

t2.start();

t3.start();

t1.join();

t2.join();

t3.join();

OUTPUT

You might also like