Skip to content

Instantly share code, notes, and snippets.

@magnusram05
Last active February 18, 2019 03:26
Show Gist options
  • Save magnusram05/11e9b4212803065db6829b8d99c453d4 to your computer and use it in GitHub Desktop.
Save magnusram05/11e9b4212803065db6829b8d99c453d4 to your computer and use it in GitHub Desktop.
Java: Inter-Thread communication
/*
Custom lock with boolean evenProceed=false
Odd thread prints, sets evenProceed to true, notifies and waits
Even thread waits in loop checking for the condition evenProceed to be true before proceeding,
prints, notifies and waits
*/
package org.practice.java.multithreading;
public class OddEvenVolatileFlavor {
public static void main(String [] args){
doWithVolatile();
}
private static void doWithVolatile(){
CustomLock lock = new CustomLock();
Thread oddThread = new OddThread(lock);
Thread evenThread = new EvenThread(lock);
oddThread.start();
evenThread.start();
try{
evenThread.join();
} catch (InterruptedException ex){
System.out.println("Main thread interrupted while waiting for " +
"Odd thread to finish execution"+ ex.getMessage());
}
}
static class CustomLock {
private boolean evenProceed = false;
public void setEvenProceed(boolean isProceed){
evenProceed = isProceed;
}
public boolean isEvenProceed(){
return this.evenProceed;
}
}
static class OddThread extends Thread {
private final CustomLock lock;
OddThread(CustomLock lock){
this.lock = lock;
}
public void run(){
synchronized(lock){
for(int i=1;i<20;i=i+2){
System.out.println("Odd thread " + i);
lock.setEvenProceed(true);
try {
lock.notify();
lock.wait();
} catch(InterruptedException ex){
System.out.println("Odd thread interrupted while waiting for " +
"Even thread notification"+ ex.getMessage());
}
}
}
}
}
static class EvenThread extends Thread {
private final CustomLock lock;
EvenThread(CustomLock lock){
this.lock = lock;
}
public void run(){
synchronized(lock){
while(!lock.isEvenProceed()){
try {
Thread.sleep(1000);
} catch(InterruptedException ex){
System.out.println("Even thread interrupted while sleeping for " +
"Odd thread to set evenProceed"+ ex.getMessage());
}
}
for(int i=2;i<=20;i=i+2){
System.out.println("Even thread" + i);
try {
lock.notify();
lock.wait();
} catch(InterruptedException ex){
System.out.println("Even thread interrupted while waiting for " +
"Odd thread notification"+ ex.getMessage());
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment