Skip to content
Snippets Groups Projects
Verified Commit c97dc349 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Add code for threads.

parent 4e532364
No related merge requests found
Pipeline #173 failed with stages
in 0 seconds
package lab;
import java.util.Queue;
import java.util.LinkedList;
public class Buffer {
private Queue<Integer> datas;
private final int maximumCapacity; // maximum size of the buffer
private int nbItems;
public Buffer(int capacity) {
maximumCapacity = capacity;
datas = new LinkedList<Integer>();
nbItems = 0;
}
public synchronized int get(int consumerNo) throws InterruptedException {
while (datas.size() == 0) {
System.out.println("No more resources! Consumer "
+ consumerNo + " waiting to draw a resource.");
wait();
}
int data = datas.remove();
--nbItems;
System.out.println("Consumer " + consumerNo + " has withdrawn "
+ data + " ==> resources : " + datas);
notifyAll();
return data;
}
public synchronized void put(int data, int producerNo) throws InterruptedException {
while (nbItems == maximumCapacity) {
System.out.println("Resources at maximum! Producer " + producerNo
+ " waiting to deposit a new resource.");
wait();
}
datas.add(data);
++nbItems;
System.out.println("Producer " + producerNo + " has deposited "
+ data + " ==> resources : " + datas);
notifyAll();
}
}
package lab;
public class TestProducerConsumer {
public static void main(String[] args) {
Buffer buffer = new Buffer(4);
Producer p1 = new Producer(buffer, 1);
Producer p2 = new Producer(buffer, 2);
Consumer c1 = new Consumer(buffer, 1);
Consumer c2 = new Consumer(buffer, 2);
p1.start();
p2.start();
c1.start();
c2.start();
}
}
/***************************************************************************************
EXECUTION EXAMPLE
Producer 1 deposited 1 ==> resources: [1]
DEPOSIT 1
Producer 1 deposited 2 ==> resources: [1, 2]
DEPOSIT 2
Consumer 1 withdrew 1 ==> resources: [2]
Producer 2 deposited 1 ==> resources: [2, 1]
DEPOSIT 1
Producer 2 deposited 2 ==> resources: [2, 1, 2]
DEPOSIT 2
Producer 2 deposited 3 ==> resources: [2, 1, 2, 3]
DEPOSIT 3
Resources at maximum! Producer 2 waiting to deposit a new resource.
CONSUME 1
Consumer 2 withdrew 2 ==> resources: [1, 2, 3]
CONSUME 2
Producer 1 deposited 3 ==> resources: [1, 2, 3, 3]
DEPOSIT 3
Resources at maximum! Producer 2 waiting to deposit a new resource.
Resources at maximum! Producer 1 waiting to deposit a new resource.
Consumer 1 withdrew 1 ==> resources: [2, 3, 3]
CONSUME 1
Producer 2 deposited 4 ==> resources: [2, 3, 3, 4]
DEPOSIT 4
Resources at maximum! Producer 1 waiting to deposit a new resource.
Resources at maximum! Producer 2 waiting to deposit a new resource.
Consumer 2 withdrew 2 ==> resources: [3, 3, 4]
CONSUME 2
Producer 1 deposited 4 ==> resources: [3, 3, 4, 4]
DEPOSIT 4
Resources at maximum! Producer 2 waiting to deposit a new resource.
Resources at maximum! Producer 1 waiting to deposit a new resource.
Consumer 1 withdrew 3 ==> resources: [3, 4, 4]
CONSUME 3
Producer 2 deposited 5 ==> resources: [3, 4, 4, 5]
DEPOSIT 5
Resources at maximum! Producer 1 waiting to deposit a new resource.
Consumer 1 withdrew 3 ==> resources: [4, 4, 5]
CONSUME 3
Producer 1 deposited 5 ==> resources: [4, 4, 5, 5]
DEPOSIT 5
Consumer 1 withdrew 4 ==> resources: [4, 5, 5]
CONSUME 4
Consumer 2 withdrew 4 ==> resources: [5, 5]
CONSUME 4
Consumer 2 withdrew 5 ==> resources: [5]
CONSUME 5
Consumer 2 withdrew 5 ==> resources: []
CONSUME 5
***************************************************************************************/
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment