Created
October 6, 2017 16:41
-
-
Save khayyamsaleem/bfebb2276022ac0ce0375b39a97f435f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//monitors | |
//EB6, Q4 | |
//E = W < S | |
import java.util.concurrent.locks.ReentrantLock; | |
import java.util.concurrent.locks.Condition; | |
public class TWS implements Runnable{ | |
private int state = 1; | |
private Condition first = new ReentrantLock().newCondition(); | |
private Condition second = new ReentrantLock().newCondition(); | |
private Condition third = new ReentrantLock().newCondition(); | |
public synchronized void first() throws InterruptedException{ | |
while(state != 1) | |
first.wait(); | |
System.out.println("first"); | |
state = 2; | |
second.signal(); | |
} | |
public synchronized void second() throws InterruptedException{ | |
while(state != 2) | |
second.wait(); | |
System.out.println("second"); | |
state = 3; | |
third.signal(); | |
} | |
public synchronized void third() throws InterruptedException{ | |
while(state != 3) | |
third.wait(); | |
System.out.println("third"); | |
state = 1; | |
first.signal(); | |
} | |
public void run(){ | |
Thread t = new Thread(new TWS()); | |
t.start(); | |
try{ | |
t.join(); | |
} catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws InterruptedException{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment