Skip to content

Commit a2a37a0

Browse files
Producer consumer
1 parent b94546e commit a2a37a0

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

Producer-Consumer/Attendent.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.time.LocalTime;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Attendent implements Runnable {
8+
9+
private Bench bench;
10+
private Integer size;
11+
12+
public Integer getSize() {
13+
return size;
14+
}
15+
16+
public void setSize(Integer size) {
17+
this.size = size;
18+
}
19+
20+
public Bench getBench() {
21+
return bench;
22+
}
23+
24+
public Attendent(Bench bench, Integer size) {
25+
super();
26+
this.bench = bench;
27+
this.size = size;
28+
}
29+
30+
public void setBench(Bench bench) {
31+
this.bench = bench;
32+
}
33+
34+
@Override
35+
public void run() {
36+
System.out.println("sending the next patient");
37+
List<Integer> patients = bench.getPatients();
38+
39+
System.out.println("Attendent" +Thread.currentThread().getName() + "stared his work at " + LocalTime.now());
40+
while (LocalTime.now().isAfter(LocalTime.of(9, 0)) && LocalTime.now().isBefore(LocalTime.of(18, 0))) {
41+
System.out.println("------------------------------------------------------------------------");
42+
synchronized (patients) {
43+
while (patients.size() == getSize()) {
44+
System.err
45+
.println("Bench is full of patients; No more patients can be added");
46+
try {
47+
patients.wait();
48+
} catch (InterruptedException e) {
49+
System.err.print(Thread.currentThread()
50+
+ " got intruppted");
51+
}
52+
}
53+
54+
int patientNbr=(int) (Math.random() * 100);
55+
patients.add(patientNbr);
56+
System.out.println(Thread.currentThread().getName() + " sending the patient to bench " + patientNbr );
57+
patients.notifyAll();
58+
System.out.println("------------------------------------------------------------------------");
59+
}
60+
}
61+
62+
}
63+
64+
}

Producer-Consumer/Bench.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.util.List;
4+
5+
public class Bench {
6+
7+
private List<Integer> patients;
8+
9+
public Bench(List<Integer> patients) {
10+
this.patients = patients;
11+
}
12+
13+
public List<Integer> getPatients() {
14+
return patients;
15+
}
16+
17+
public void setPatients(List<Integer> patients) {
18+
this.patients = patients;
19+
}
20+
21+
}

Producer-Consumer/Doctor.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.time.LocalTime;
4+
import java.util.List;
5+
6+
public class Doctor implements Runnable {
7+
8+
private Bench bench;
9+
private Integer size;
10+
11+
public Integer getSize() {
12+
return size;
13+
}
14+
15+
public void setSize(Integer size) {
16+
this.size = size;
17+
}
18+
19+
public Bench getBench() {
20+
return bench;
21+
}
22+
23+
public Doctor(Bench bench, Integer size) {
24+
super();
25+
this.bench = bench;
26+
this.size = size;
27+
}
28+
29+
public void setBench(Bench bench) {
30+
this.bench = bench;
31+
}
32+
33+
@Override
34+
public void run() {
35+
System.out.println("sending the next patient");
36+
List<Integer> patients = bench.getPatients();
37+
System.out.println("Doctor" +Thread.currentThread().getName() +"stared his work at " + LocalTime.now());
38+
while (LocalTime.now().isAfter(LocalTime.of(9, 0))
39+
&& LocalTime.now().isBefore(LocalTime.of(18, 0))) {
40+
System.out.println("------------------------------------------------------------------------");
41+
synchronized (patients) {
42+
while (patients.size() == 0) {
43+
System.out
44+
.println("Bench is empty of patients; No more patients available to treat " + Thread.currentThread().getName() + " is waiting");
45+
try {
46+
patients.wait();
47+
} catch (InterruptedException e) {
48+
System.err.print(Thread.currentThread()
49+
+ " got intruppted");
50+
}
51+
}
52+
53+
System.out.println(Thread.currentThread().getName() + " took patient from bench to treat "
54+
+ patients.get(patients.size() - 1));
55+
try {
56+
Thread.sleep(2000L);
57+
} catch (InterruptedException e) {
58+
e.printStackTrace();
59+
}
60+
patients.remove(patients.size() - 1);
61+
patients.notifyAll();
62+
System.out.println("------------------------------------------------------------------------");
63+
}
64+
}
65+
66+
}
67+
68+
}

Producer-Consumer/Hospital.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.util.ArrayList;
4+
5+
public class Hospital {
6+
7+
public static void main(String[] args) {
8+
9+
final Integer SIZE=10;
10+
11+
Bench bench=new Bench(new ArrayList<Integer>(SIZE));
12+
13+
Attendent attendent=new Attendent(bench,SIZE);
14+
Thread attendentThread=new Thread(attendent);
15+
attendentThread.setName("Raju");
16+
attendentThread.start();
17+
18+
19+
20+
Doctor doctor=new Doctor(bench,SIZE);
21+
Thread docThread=new Thread(doctor);
22+
docThread.setName("Sondhi");
23+
docThread.start();
24+
25+
26+
Doctor doctor1=new Doctor(bench,SIZE);
27+
Thread docThread1=new Thread(doctor1);
28+
docThread1.setName("Vivek");
29+
docThread1.start();
30+
31+
}
32+
}

0 commit comments

Comments
 (0)