Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
aboikliev committed May 12, 2015
1 parent 9633716 commit 8ad6466
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 35 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
public class ConsumerThread implements Runnable {
private MyBlockingQueue<String> queue;

private String name;

public ConsumerThread(String name, MyBlockingQueue<String> queue) {
public ConsumerThread(MyBlockingQueue<String> queue) {
this.queue = queue;
this.name = name;
}

@Override
Expand All @@ -17,8 +14,4 @@ public void run() {
queue.poll();
}
}

String getName() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@
public class ProducerThread implements Runnable {

private MyBlockingQueue<String> queue;
private String name;

public ProducerThread(String name, MyBlockingQueue<String> queue) {
public ProducerThread(MyBlockingQueue<String> 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;
}
}
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();

}
}

0 comments on commit 8ad6466

Please sign in to comment.