-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFairSem_Test.java
69 lines (57 loc) · 1.67 KB
/
FairSem_Test.java
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
59
60
61
62
63
64
65
66
67
68
69
class testingThread_FS extends Thread{
FairSem fs;
long waitingMillis;
testingThread_FS(FairSem fs, long waitingMillis)
{
this.fs = fs;
this.waitingMillis = waitingMillis;
System.out.println("Thread "+this.getId()+":\tcreated");
}
public void run()
{
try {
fs.P();
} catch (InterruptedException ie){
System.err.println("InterruptedException waiting");
};
//System.out.println("Thread "+this.getId()+":\twaiting some time...");
try {
Thread.sleep(waitingMillis);
} catch (InterruptedException ie) {
System.err.println("InterruptedException sleeping");
}
System.out.println("Thread "+this.getId()+":\tEsecuzione Completata");
fs.V();
}
}
public class FairSem_Test{
private static FairSem fs;
public static void main(String[] args){
System.out.println("Main: starting");
int val =2;
int num_thread = 10;
System.out.println();
System.out.println("-----------------------------");
System.out.println("Semaforo inizializzato a "+val);
System.out.println("Numero dei thread: "+num_thread);
System.out.println("-----------------------------");
System.out.println();
fs = new FairSem(val, true);
testingThread_FS thread_array[] = new testingThread_FS[num_thread];
//Creazione dei thread
for (int i=0; i<num_thread; i++)
thread_array[i] = new testingThread_FS(fs, 10);
//Avvio dei thread
for (int i=0; i<num_thread; i++)
thread_array[i].start();
//Attesa dei thread
for (int i=0; i<num_thread; i++) {
try {
thread_array[i].join();
} catch (InterruptedException ie) {
System.err.println("Main: InterruptedException calling join method");
}
}
System.out.println("Main: finishing");
}
}