Skip to content

Instantly share code, notes, and snippets.

@edgardleal
Created April 30, 2018 00:55
Show Gist options
  • Save edgardleal/bc0d6163b995feb9bff30619ef6fe430 to your computer and use it in GitHub Desktop.
Save edgardleal/bc0d6163b995feb9bff30619ef6fe430 to your computer and use it in GitHub Desktop.
Three Java Threads running in parallel and synchronized
import java.util.LinkedList;
import java.util.List;
/**
* <p>
* This class will start a Thread to generate a certain number os random numbers and
* other Thread to accumulate odd numbers and other Thread to accumulate even numbers.
*
* Each Thread will wait for previous Thread to make its action.
*
* This isn't the best solution for performance but the intent of this code is to demonstrate simple
* way to make tree Threads working in parallel and synchronized.
* </p>
*
* @author edgardleal
*/
public class Main {
static Object evenLocker = new Object();
static Object oddLocker = new Object();
static Object generatorLocker = new Object();
private static int currentValue;
public static void debug(String text) {
System.out.println(text);
}
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
Generator generator = new Generator(40, main);
OddThread oddThread = new OddThread();
EvenThread evenThread = new EvenThread(main);
generator.start();
oddThread.start();
evenThread.start();
synchronized (generatorLocker) {
Thread.sleep(100); // wait all thread start
debug("Starting generator...");
generatorLocker.notify();
}
synchronized (main) {
main.wait();
}
oddThread.finish();
evenThread.finish();
synchronized (oddLocker) {
oddLocker.notify();
}
System.out.print("Odd results : ");
oddThread.print();
System.out.print("Even results: ");
evenThread.print();
}
static class Generator extends Thread {
private final Main main;
private int total;
public Generator(int total, Main main) {
this.total = total;
this.main = main;
}
@Override
public void run() {
for (int i = 0; i < total; i++) {
try {
synchronized (generatorLocker) {
generatorLocker.wait();
}
Main.currentValue = (int) Math.round(Math.random() * total);
synchronized (oddLocker) {
oddLocker.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} // for
synchronized (oddLocker) {
oddLocker.notify();
}
synchronized (main) {
main.notify();
}
debug("Generation finished !");
}
}
static class OddThread extends Thread {
boolean finished = false;
protected List<Integer> values = new LinkedList<>();
public void print() {
for (int value : values) {
System.out.print(value + " ");
}
System.out.println("");
}
public synchronized void finish() {
this.finished = true;
}
@Override
public void run() {
while (!finished) {
try {
synchronized (oddLocker) {
oddLocker.wait();
}
if (!finished && Main.currentValue % 2 != 0) {
this.values.add(Main.currentValue);
}
synchronized (evenLocker) {
evenLocker.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
debug("OddThread Finished!");
}
} // Odd Thread
static class EvenThread extends OddThread {
private Main main;
public EvenThread(Main main) {
this.main = main;
}
@Override
public void run() {
while (!finished) {
try {
synchronized (evenLocker) {
evenLocker.wait();
}
if (!finished && Main.currentValue % 2 == 0) {
this.values.add(Main.currentValue);
}
synchronized (generatorLocker) {
generatorLocker.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
debug("EvenThread Finished!");
synchronized (main) {
main.notify();
}
synchronized (oddLocker) {
oddLocker.notify();
}
}
} // EvenThread
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment