-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSemaphoreExample.java
More file actions
58 lines (50 loc) · 2.12 KB
/
SemaphoreExample.java
File metadata and controls
58 lines (50 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package rafael.q.g.petri_network_examples;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author rafael queiroz goncalves
*/
public class SemaphoreExample {
private static Semaphore semaphore;
private static Semaphore sinchronization;
private static String output="";//shared variable
public static void main(String[] args) throws InterruptedException{
byte slotLimit=1;
byte nThreads=10;
sinchronization = new Semaphore(0);
semaphore = new Semaphore(slotLimit);
System.out.println("::: FORK PROCESSING :::");
for(byte i=0;i<nThreads;i++){
new ThreadProcessingAlpha(i).start();
}
//join point to all threads
sinchronization.acquire(nThreads); //wait until the semaphore has "n" tokens to go on
System.out.println("::: JOIN POINT TO ALL THREADS :::");
System.out.println(output);
}
private static class ThreadProcessingAlpha extends Thread {
short threadId;
public ThreadProcessingAlpha(short threadId){
this.threadId=threadId;
}
@Override
public void run(){
try {
System.out.println("Thread "+ threadId+" - Initiating....");
//some particular logic
output+="Thread "+ threadId+" - waiting to enter in critical region\n";//DANGEROUS: lost assignement
semaphore.acquire(); // this instruction is blocking
//critical region
output+="CRITICAL: Thread "+ threadId+" - entering critical region\n";
output+="CRITICAL: Thread "+ threadId+" - exiting critical region\n";
semaphore.release();
//some particular logic
sinchronization.release();//add one permit/token to the semaphore aiming the thread join.
} catch (InterruptedException ex) {
Logger.getLogger(SemaphoreExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}