-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
35 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.hackbulgaria.corejava; | ||
|
||
public class WaitNotifyMechanism { | ||
public static long startTime = System.currentTimeMillis(); | ||
public static Integer counter = 0; | ||
public static final Object monitor = new Object(); | ||
private static int turn = 0; | ||
|
||
public static void increment() { | ||
System.out.println("Incrementing from Thread : " + Thread.currentThread().getName() + " " + counter ); | ||
counter++; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Thread t1 = new Thread() { | ||
public void run() { | ||
for (int i = 0; i < 2_000_000; i++) { | ||
synchronized (monitor) { | ||
while (turn != 1) { | ||
try { | ||
monitor.wait(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
increment(); | ||
|
||
turn = (turn + 1) % 2; | ||
monitor.notify(); | ||
} | ||
|
||
} | ||
} | ||
}; | ||
Thread t2 = new Thread() { | ||
|
||
public void run() { | ||
for (int i = 0; i < 2_000_000; i++) { | ||
synchronized (monitor) { | ||
while (turn != 0) { | ||
try { | ||
monitor.wait(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
increment(); | ||
|
||
turn = (turn + 1) % 2; | ||
monitor.notify(); | ||
} | ||
} | ||
} | ||
}; | ||
t1.setName("T1"); | ||
t2.setName("T2"); | ||
t1.start(); | ||
t2.start(); | ||
t1.join(); | ||
t2.join(); | ||
System.out.println(counter); | ||
System.out.println(System.currentTimeMillis() - startTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 23 additions & 17 deletions
40
week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
package week6.task2.producer_consumer; | ||
|
||
public class ThreadRunner { | ||
public static void main(String[] args) { | ||
public static void main(String[] args) throws InterruptedException { | ||
MyBlockingQueue<String> queue = new MyBlockingQueue<>(100); | ||
ProducerThread p1 = new ProducerThread("producer 1", queue); | ||
ProducerThread p2 = new ProducerThread("producer 2", queue); | ||
ProducerThread p3 = new ProducerThread("producer 3", queue); | ||
ConsumerThread c1 = new ConsumerThread("consumer 1", queue); | ||
ConsumerThread c2 = new ConsumerThread("consumer 2", queue); | ||
|
||
ProducerThread p1 = new ProducerThread(queue); | ||
ProducerThread p2 = new ProducerThread(queue); | ||
ProducerThread p3 = new ProducerThread(queue); | ||
|
||
ConsumerThread c1 = new ConsumerThread(queue); | ||
ConsumerThread c2 = new ConsumerThread(queue); | ||
|
||
Thread t1 = new Thread(p1); | ||
t1.setName(p1.getName()); | ||
t1.setName("producer 1"); | ||
Thread t2 = new Thread(p2); | ||
t2.setName(p2.getName()); | ||
t2.setName("producer 2"); | ||
Thread t3 = new Thread(p3); | ||
t3.setName(p3.getName()); | ||
t3.setName("producer 3"); | ||
|
||
Thread t4 = new Thread(c1); | ||
t4.setName(c1.getName()); | ||
t4.setName("consumer 1"); | ||
Thread t5 = new Thread(c2); | ||
t5.setName(c2.getName()); | ||
|
||
|
||
t5.setName("consumer 2"); | ||
|
||
t1.start(); | ||
t2.start(); | ||
t3.start(); | ||
t4.start(); | ||
t5.start(); | ||
// t1.join(); | ||
// t2.join(); | ||
// t3.join(); | ||
// t4.join(); | ||
// t5.join(); | ||
|
||
} | ||
} |