Skip to content

Commit 0d3a709

Browse files
Producer Consumer Using BlockingQueue
1 parent a2a37a0 commit 0d3a709

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import java.util.concurrent.LinkedBlockingQueue;
7+
8+
public class Attendent implements Runnable {
9+
10+
private Bench bench;
11+
12+
public Bench getBench() {
13+
return bench;
14+
}
15+
16+
public Attendent(Bench bench) {
17+
super();
18+
this.bench = bench;
19+
}
20+
21+
public void setBench(Bench bench) {
22+
this.bench = bench;
23+
}
24+
25+
@Override
26+
public void run() {
27+
28+
LinkedBlockingQueue<Integer> patients= bench.getPatients();
29+
30+
System.out.println("Attendent" +Thread.currentThread().getName() + "stared his work at " + LocalTime.now());
31+
while (LocalTime.now().isAfter(LocalTime.of(9, 0)) && LocalTime.now().isBefore(LocalTime.of(18, 0))) {
32+
//System.out.println("------------------------------------------------------------------------");
33+
34+
int patientNbr=(int) (Math.random() * 100);
35+
try {
36+
patients.put(patientNbr);
37+
System.out.println(Thread.currentThread().getName() + " sending the patient to bench " + patientNbr );
38+
Thread.sleep(2000L);
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
}
42+
43+
44+
//System.out.println("------------------------------------------------------------------------");
45+
}
46+
}
47+
48+
}
49+
50+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.util.List;
4+
import java.util.Queue;
5+
import java.util.concurrent.LinkedBlockingQueue;
6+
7+
public class Bench {
8+
9+
private LinkedBlockingQueue<Integer> patients;
10+
11+
public Bench(LinkedBlockingQueue<Integer> patients) {
12+
this.patients = patients;
13+
}
14+
15+
public LinkedBlockingQueue<Integer> getPatients() {
16+
return patients;
17+
}
18+
19+
public void setPatients(LinkedBlockingQueue<Integer> patients) {
20+
this.patients = patients;
21+
}
22+
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.time.LocalTime;
4+
import java.util.List;
5+
import java.util.Queue;
6+
import java.util.concurrent.LinkedBlockingQueue;
7+
8+
public class Doctor implements Runnable {
9+
10+
private Bench bench;
11+
12+
public Bench getBench() {
13+
return bench;
14+
}
15+
16+
public Doctor(Bench bench) {
17+
super();
18+
this.bench = bench;
19+
20+
}
21+
22+
public void setBench(Bench bench) {
23+
this.bench = bench;
24+
}
25+
26+
@Override
27+
public void run() {
28+
LinkedBlockingQueue<Integer> patients = bench.getPatients();
29+
System.out.println("Doctor" +Thread.currentThread().getName() +"stared his work at " + LocalTime.now());
30+
while (LocalTime.now().isAfter(LocalTime.of(9, 0))
31+
&& LocalTime.now().isBefore(LocalTime.of(18, 0))) {
32+
System.out.println("------------------------------------------------------------------------");
33+
34+
try {
35+
System.out.println(Thread.currentThread().getName() + " took patient from bench to treat " + patients.take());
36+
Thread.sleep(2000L);
37+
} catch (InterruptedException e1) {
38+
e1.printStackTrace();
39+
40+
System.out.println("------------------------------------------------------------------------");
41+
}
42+
}
43+
44+
}}
45+
46+
47+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.wipo.suite.modules.administration.clients.pc;
2+
3+
import java.util.ArrayList;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
public class Hospital {
7+
8+
public static void main(String[] args) {
9+
10+
11+
12+
Bench bench=new Bench(new LinkedBlockingQueue<Integer>());
13+
14+
Attendent attendent=new Attendent(bench);
15+
Thread attendentThread=new Thread(attendent);
16+
attendentThread.setName("Raju");
17+
attendentThread.start();
18+
19+
20+
21+
Doctor doctor=new Doctor(bench);
22+
Thread docThread=new Thread(doctor);
23+
docThread.setName("Sondhi");
24+
docThread.start();
25+
26+
27+
Doctor doctor1=new Doctor(bench);
28+
Thread docThread1=new Thread(doctor1);
29+
docThread1.setName("Vivek");
30+
docThread1.start();
31+
32+
}
33+
}

0 commit comments

Comments
 (0)