Created
January 31, 2012 22:30
-
-
Save Feder1co5oave/1713466 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @author Feder1co 5oave | |
*/ | |
class Schedulatore { | |
private final int N; | |
private String[] seq; | |
private int corrente; | |
private boolean fine; | |
public Schedulatore(int num) { | |
N = num; | |
corrente = num; | |
seq = new String[num]; | |
fine = false; | |
} | |
public synchronized void nuovaSequenza(String[] seq) { | |
try { | |
while (corrente != N) wait(); | |
} catch (InterruptedException ex) {} | |
for (int i = 0; i < N; i++) this.seq[i] = seq[i]; | |
corrente = 0; | |
notifyAll(); | |
} | |
public synchronized void fineEsecuzione() { | |
fine = true; | |
} | |
public synchronized void aspettaTurno(String nome) { | |
try { | |
while (corrente == N || !nome.equals(seq[corrente])) | |
wait(); | |
} catch (InterruptedException ex) {} | |
} | |
public synchronized boolean prossimoTurno() { | |
corrente++; | |
notifyAll(); | |
return fine; | |
} | |
} |
This file contains 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
class SchedulerThread extends Thread { | |
Schedulatore sc; | |
public SchedulerThread(Schedulatore aSched) { | |
sc = aSched; | |
// System.out.println("scheduler created"); | |
} | |
public void run() { | |
String s[] = new String[4]; | |
// System.out.println("scheduler: 1st schedule"); | |
s[0] = "paperino"; s[1] = "topolino"; s[2] = "pippo"; s[3] = "pluto"; | |
sc.nuovaSequenza(s); | |
// System.out.println("scheduler: 2st schedule"); | |
s[0] = "topolino"; s[1] = "pluto"; s[2] = "pippo"; s[3] = "paperino"; | |
sc.nuovaSequenza(s); | |
// System.out.println("scheduler: 3st schedule"); | |
s[0] = "pluto"; s[1] = "paperino"; s[2] = "pippo"; s[3] = "topolino"; | |
sc.nuovaSequenza(s); | |
sc.fineEsecuzione(); | |
} | |
} | |
class WorkerThread extends Thread { | |
Schedulatore sch; | |
public WorkerThread(String name, Schedulatore aSched) { | |
super(name); | |
sch= aSched; | |
// System.out.println(getName() + " created"); | |
} | |
public void run() { | |
while (true) { | |
sch.aspettaTurno(getName()); | |
System.out.println(getName()); | |
try { | |
sleep(200); | |
} catch(InterruptedException e){} | |
if (sch.prossimoTurno()) break; | |
} | |
} | |
} | |
public class Pesa { | |
public static void main(String[] args) throws InterruptedException { | |
final int N = 4; | |
String names[] = {"pippo", "pluto", "paperino", "topolino"}; | |
Schedulatore manager = new Schedulatore(N); | |
Thread sched = new SchedulerThread(manager); | |
sched.start(); | |
Thread workers[] = new Thread[N]; | |
for (int i = 0; i < N; i++) { | |
workers[i] = new WorkerThread(names[i], manager); | |
workers[i].start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment