Last active
September 3, 2019 05:49
-
-
Save unnisworld/c27d4683eb056435d654e4723d53f5a5 to your computer and use it in GitHub Desktop.
PrintEvenOdd
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
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