MULTITHREADING
INGOT MARULAM D.S (10913)
ERIKA ARIANA(10863)
public class program
{
private nodeadlock mutex, full, empty;
private int in, out;
private int buffer[];
private static final int BUFFER_SIZE = 3;
public program()
{
in = 0;
out = 0;
buffer = new int[BUFFER_SIZE];
mutex = new nodeadlock(1);
empty = new nodeadlock(BUFFER_SIZE);
full = new nodeadlock(0);
}
public void write(int value)
{
empty.waits();
mutex.waits();
buffer[in] = value;
in = (in+1)%BUFFER_SIZE;
System.out.println("Produser menambah: "+value);
mutex.signals();
full.signals();
}
public int read()
{
full.waits();
mutex.waits();
int readValue = buffer[out];
out = (out+1)%BUFFER_SIZE;
System.out.println("Konsumen mengambil: "+readValue);
mutex.signals();
empty.signals();
return readValue;
}
}
public class nodeadlock
{
private int value;
public nodeadlock()
{
value = 0;
}
public nodeadlock(int value)
{
this.value = value;
}
public synchronized void waits()
{
while(value == 0)
{
try
{
wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
value--;
}
public synchronized void signals()
{
value++;
notifyAll();
}
}
public class Consumer extends Thread
{
private program buffer;
public Consumer(program buffer)
{
this.buffer = buffer;
}
public void run()
{
int sum = 0;
for(int count=1; count<=9999; count++)
{
try
{
Thread.sleep((int)(Math.random()*2000));
System.out.println("Konsumen coba mengkonsumsi");
sum += buffer.read();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Konsumen selesai mengkonsumsi");
System.out.println("Data yang dikonsumsi konsumen: "+sum);
}
}
public class Producer extends Thread
{
private program buffer;
public Producer(program buffer)
{
this.buffer = buffer;
}
public void run()
{
for(int count = 1; count <= 9999; count++)
{
try
{
Thread.sleep((int)(Math.random()*2000));
System.out.println("Produser coba memproduksi:
"+count);
buffer.write(count);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Produser selesai memproduksi");
}
}
public class main
{
public static void main(String[] args)
{
program buffer = new program();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
producer.start();
consumer.start();
}
}
OUTPUT :
init:
deps-jar:
Compiling 5 source files to C:\Documents and Settings\erika_ariana\JavaApplication7\build\classes
compile:
run:
Konsumen coba mengkonsumsi
Produser coba memproduksi: 1
Produser menambah: 1
Konsumen mengambil: 1
Konsumen coba mengkonsumsi
Produser coba memproduksi: 2
Produser menambah: 2
Konsumen mengambil: 2
Produser coba memproduksi: 3
Produser menambah: 3
Konsumen coba mengkonsumsi
Konsumen mengambil: 3
Konsumen coba mengkonsumsi
Produser coba memproduksi: 4
Produser menambah: 4
Konsumen mengambil: 4
Produser coba memproduksi: 5
Produser menambah: 5
Konsumen coba mengkonsumsi
Konsumen mengambil: 5
Produser coba memproduksi: 6
Produser menambah: 6
Konsumen coba mengkonsumsi
Konsumen mengambil: 6
Konsumen coba mengkonsumsi
Produser coba memproduksi: 7
Produser menambah: 7
DST…