Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (2)
# An example Java 11 project using Maven and JUnit 5.x
# An example Java 17 project using Maven and JUnit 5.x
1. Get adoptopenjdk-11: https://adoptopenjdk.net/
1. Get adoptopenjdk-17: https://adoptopenjdk.net/
2. Get maven 3.6.x: https://maven.apache.org/
3. Add above to your path if neccessary.
4. `git clone git@github.com:example/test.git`
5. `mvn clean package`
6. `java -jar target/efrei-lab07-generics-empty-0.0.1-SNAPSHOT.jar`
6. `java -jar target/efrei-lab09-threads-0.0.1-SNAPSHOT.jar`
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
***************************************************************************************/