Skip to content

Instantly share code, notes, and snippets.

@unnisworld
Last active September 3, 2019 05:49
Show Gist options
  • Save unnisworld/c27d4683eb056435d654e4723d53f5a5 to your computer and use it in GitHub Desktop.
Save unnisworld/c27d4683eb056435d654e4723d53f5a5 to your computer and use it in GitHub Desktop.
PrintEvenOdd
package corejava;
public class PrintEvenOddApproachOne {
private static volatile int count = 1;
private static final int max = 100;
private static volatile boolean odd = true;
private static Object lock = new Object();
public static void main(String[] args) throws Exception {
Thread evenThread = new Thread(new EvenPrinter());
Thread oddThread = new Thread(new OddPrinter());
evenThread.start();
oddThread.start();
evenThread.join();
oddThread.join();
}
static class EvenPrinter implements Runnable {
@Override
public void run() {
while (count < max) {
// wait till notified or "odd" flag's value is flipped.
while(odd) {
try {
synchronized(lock) {
// wait with a timeout to avoid indefinite wait scenario
lock.wait(5);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} // end of wait
// Print next even value
System.out.println(count);
count++;
// flip flag value
odd = true;
// Notify other thread
synchronized(lock) {
lock.notifyAll();
}
}
}
}
static class OddPrinter implements Runnable {
@Override
public void run() {
while(count < max) {
// wait till notified or "odd" flag's value is flipped.
while(!odd) {
try {
synchronized(lock) {
// wait with a timeout to avoid indefinite wait scenario
lock.wait(5);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} // end of wait
// Print next odd value
System.out.println(count);
count++;
// flip flag value
odd = false;
// Notify other thread
synchronized(lock) {
lock.notifyAll();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment