Skip to content

Commit 28e66b6

Browse files
committed
commit
1 parent c88b537 commit 28e66b6

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.hackbulgaria.corejava;
2+
3+
public class WaitNotifyMechanism {
4+
public static long startTime = System.currentTimeMillis();
5+
public static Integer counter = 0;
6+
public static final Object monitor = new Object();
7+
private static int turn = 0;
8+
9+
public static void increment() {
10+
System.out.println("Incrementing from Thread : " + Thread.currentThread().getName() + " " + counter );
11+
counter++;
12+
}
13+
14+
public static void main(String[] args) throws InterruptedException {
15+
Thread t1 = new Thread() {
16+
public void run() {
17+
for (int i = 0; i < 2_000_000; i++) {
18+
synchronized (monitor) {
19+
while (turn != 1) {
20+
try {
21+
monitor.wait();
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
26+
}
27+
increment();
28+
29+
turn = (turn + 1) % 2;
30+
monitor.notify();
31+
}
32+
33+
}
34+
}
35+
};
36+
Thread t2 = new Thread() {
37+
38+
public void run() {
39+
for (int i = 0; i < 2_000_000; i++) {
40+
synchronized (monitor) {
41+
while (turn != 0) {
42+
try {
43+
monitor.wait();
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
49+
increment();
50+
51+
turn = (turn + 1) % 2;
52+
monitor.notify();
53+
}
54+
}
55+
}
56+
};
57+
t1.setName("T1");
58+
t2.setName("T2");
59+
t1.start();
60+
t2.start();
61+
t1.join();
62+
t2.join();
63+
System.out.println(counter);
64+
System.out.println(System.currentTimeMillis() - startTime);
65+
}
66+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package week6.task2.producer_consumer;
2+
3+
4+
public class ConsumerThread implements Runnable {
5+
private MyBlockingQueue<String> queue;
6+
7+
public ConsumerThread(MyBlockingQueue<String> queue) {
8+
this.queue = queue;
9+
}
10+
11+
@Override
12+
public void run() {
13+
while (queue.size() > 0) {
14+
queue.poll();
15+
}
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package week6.task2.producer_consumer;
2+
3+
public class ProducerThread implements Runnable {
4+
5+
private MyBlockingQueue<String> queue;
6+
public ProducerThread(MyBlockingQueue<String> queue) {
7+
this.queue = queue;
8+
9+
}
10+
11+
@Override
12+
public void run() {
13+
for (int i = 1; i < 1001; i++) {
14+
queue.add(Thread.currentThread().getName() + ": " + i + " - " + System.nanoTime()
15+
% 1_000_000);
16+
}
17+
}
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package week6.task2.producer_consumer;
2+
3+
public class ThreadRunner {
4+
public static void main(String[] args) throws InterruptedException {
5+
MyBlockingQueue<String> queue = new MyBlockingQueue<>(100);
6+
7+
ProducerThread p1 = new ProducerThread(queue);
8+
ProducerThread p2 = new ProducerThread(queue);
9+
ProducerThread p3 = new ProducerThread(queue);
10+
11+
ConsumerThread c1 = new ConsumerThread(queue);
12+
ConsumerThread c2 = new ConsumerThread(queue);
13+
14+
Thread t1 = new Thread(p1);
15+
t1.setName("producer 1");
16+
Thread t2 = new Thread(p2);
17+
t2.setName("producer 2");
18+
Thread t3 = new Thread(p3);
19+
t3.setName("producer 3");
20+
21+
Thread t4 = new Thread(c1);
22+
t4.setName("consumer 1");
23+
Thread t5 = new Thread(c2);
24+
t5.setName("consumer 2");
25+
26+
t1.start();
27+
t2.start();
28+
t3.start();
29+
t4.start();
30+
t5.start();
31+
// t1.join();
32+
// t2.join();
33+
// t3.join();
34+
// t4.join();
35+
// t5.join();
36+
37+
}
38+
}

0 commit comments

Comments
 (0)