diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java b/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java new file mode 100644 index 0000000..95eacc6 --- /dev/null +++ b/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java @@ -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); + } +} diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java index 9b8a3a2..bc0a660 100644 --- a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java @@ -4,11 +4,8 @@ public class ConsumerThread implements Runnable { private MyBlockingQueue queue; - private String name; - - public ConsumerThread(String name, MyBlockingQueue queue) { + public ConsumerThread(MyBlockingQueue queue) { this.queue = queue; - this.name = name; } @Override @@ -17,8 +14,4 @@ public void run() { queue.poll(); } } - - String getName() { - return this.name; - } } diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java index c8d4c88..72927dc 100644 --- a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java @@ -3,23 +3,16 @@ public class ProducerThread implements Runnable { private MyBlockingQueue queue; - private String name; - - public ProducerThread(String name, MyBlockingQueue queue) { + public ProducerThread(MyBlockingQueue queue) { this.queue = queue; - this.name = name; } @Override public void run() { - for (int i = 0; i < 1000; i++) { - queue.add(this.name + ": " + i + " - " + System.nanoTime() + for (int i = 1; i < 1001; i++) { + queue.add(Thread.currentThread().getName() + ": " + i + " - " + System.nanoTime() % 1_000_000); } } - - String getName() { - return this.name; - } } diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java index 28f91d7..3810328 100644 --- a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java @@ -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 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(); + } }