Skip to content

Commit 8ad6466

Browse files
committed
rework
1 parent 9633716 commit 8ad6466

File tree

4 files changed

+93
-35
lines changed

4 files changed

+93
-35
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+
}

week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
public class ConsumerThread implements Runnable {
55
private MyBlockingQueue<String> queue;
66

7-
private String name;
8-
9-
public ConsumerThread(String name, MyBlockingQueue<String> queue) {
7+
public ConsumerThread(MyBlockingQueue<String> queue) {
108
this.queue = queue;
11-
this.name = name;
129
}
1310

1411
@Override
@@ -17,8 +14,4 @@ public void run() {
1714
queue.poll();
1815
}
1916
}
20-
21-
String getName() {
22-
return this.name;
23-
}
2417
}

week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@
33
public class ProducerThread implements Runnable {
44

55
private MyBlockingQueue<String> queue;
6-
private String name;
7-
8-
public ProducerThread(String name, MyBlockingQueue<String> queue) {
6+
public ProducerThread(MyBlockingQueue<String> queue) {
97
this.queue = queue;
108

11-
this.name = name;
129
}
1310

1411
@Override
1512
public void run() {
16-
for (int i = 0; i < 1000; i++) {
17-
queue.add(this.name + ": " + i + " - " + System.nanoTime()
13+
for (int i = 1; i < 1001; i++) {
14+
queue.add(Thread.currentThread().getName() + ": " + i + " - " + System.nanoTime()
1815
% 1_000_000);
1916
}
2017
}
21-
22-
String getName() {
23-
return this.name;
24-
}
2518
}
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package week6.task2.producer_consumer;
2+
23
public class ThreadRunner {
3-
public static void main(String[] args) {
4+
public static void main(String[] args) throws InterruptedException {
45
MyBlockingQueue<String> queue = new MyBlockingQueue<>(100);
5-
6-
ProducerThread p1 = new ProducerThread("producer 1", queue);
7-
ProducerThread p2 = new ProducerThread("producer 2", queue);
8-
ProducerThread p3 = new ProducerThread("producer 3", queue);
9-
10-
ConsumerThread c1 = new ConsumerThread("consumer 1", queue);
11-
ConsumerThread c2 = new ConsumerThread("consumer 2", queue);
12-
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+
1314
Thread t1 = new Thread(p1);
14-
t1.setName(p1.getName());
15+
t1.setName("producer 1");
1516
Thread t2 = new Thread(p2);
16-
t2.setName(p2.getName());
17+
t2.setName("producer 2");
1718
Thread t3 = new Thread(p3);
18-
t3.setName(p3.getName());
19-
19+
t3.setName("producer 3");
20+
2021
Thread t4 = new Thread(c1);
21-
t4.setName(c1.getName());
22+
t4.setName("consumer 1");
2223
Thread t5 = new Thread(c2);
23-
t5.setName(c2.getName());
24-
25-
24+
t5.setName("consumer 2");
25+
2626
t1.start();
2727
t2.start();
2828
t3.start();
2929
t4.start();
3030
t5.start();
31+
// t1.join();
32+
// t2.join();
33+
// t3.join();
34+
// t4.join();
35+
// t5.join();
36+
3137
}
3238
}

0 commit comments

Comments
 (0)