Created
June 4, 2018 15:43
-
-
Save OrenBochman/b567e4682be9927f8ee4a47753264f30 to your computer and use it in GitHub Desktop.
junit5
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
import java.util.concurrent.Semaphore; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import org.junit.jupiter.api.DisplayName; | |
import org.junit.jupiter.api.RepeatedTest; | |
import org.junit.jupiter.api.Test; | |
@DisplayName("Little book of semaphores - Rendevous") | |
class Rendevous extends Thread { | |
// public static void main(String[] args) { | |
// Semaphore aDone = new Semaphore(0); | |
// Semaphore bDone = new Semaphore(0); | |
// new Rendevous("a",aDone,bDone).start(); | |
// new Rendevous("b",bDone,aDone).start(); | |
// } | |
//@RepeatedTest(4) // Dynamic JavaScript expression. | |
@Test | |
@DisplayName("Rendevous a before b") | |
void test1(){ | |
Semaphore aDone = new Semaphore(0); | |
Semaphore bDone = new Semaphore(0); | |
new Rendevous("a",aDone,bDone).start(); | |
new Rendevous("b",bDone,aDone).start(); | |
} | |
@DisplayName("Rendevous b before a") | |
@Test | |
//@RepeatedTest(4) // Dynamic JavaScript expression. | |
void test2(){ | |
Semaphore aDone = new Semaphore(0); | |
Semaphore bDone = new Semaphore(0); | |
new Rendevous("b",bDone,aDone).start(); | |
new Rendevous("a",aDone,bDone).start(); | |
} | |
private String letter; | |
private Semaphore iArrived; | |
private Semaphore uArrived; | |
Rendevous(String letter,Semaphore myMutex,Semaphore otherMutex){ | |
this.letter=letter; | |
this.iArrived=myMutex; | |
this.uArrived=otherMutex; | |
} | |
@Override | |
public void run() { | |
step1(); | |
try { | |
iArrived.release(); //block | |
uArrived.acquire(); //signal | |
} catch (InterruptedException e) { | |
System.out.println(letter+" interrupted"); | |
} | |
step2(); | |
} | |
void step1(){ | |
System.out.println(letter+"1 done"); | |
} | |
void step2() { | |
System.out.println(letter+"2 done"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment