[ Pobierz całość w formacie PDF ]
.length;private Object runLock = new Object(),fairLock = new Object(),outLock = new Object(),stopLock = new Object();private Thread[] thread = new Thread [count];private int[] tally = new int [count];private boolean playOver = false;private int limit;public Program(){synchronized (fairLock) {synchronized (runLock) {for (int i = 0; i < count ; i++) {runId = i;thread[i] = new Runner();try {runLock.wait();}catch (InterruptedException e) {}}}}}class Runnerextends Thread {public Runner(){start();}public void run(){int id = 0;synchronized (runLock) {id = runId;runLock.notify();}synchronized (fairLock) {}if (id == 0)timerRun();elsenameRun(id);}}public void sleep(int timeMillis){try {Thread.sleep(timeMillis);}catch (InterruptedException e) {}}public void timerRun(){sleep(1000);playOver = true;System.out.println();for (int i = 0; i < count ; i++) {synchronized (stopLock) {thread[i].interrupt();if (i != 0)System.out.println(name[i] + ": " + tally[i]);}}}public void nameRun(int id){synchronized (outLock) {System.out.println("\n" + name[id] + " starts");}while (true) {synchronized (stopLock) {Thread thisThread =Thread.currentThread();if (thisThread.isInterrupted())return;if (!playOver) {tally[id]++;synchronized (outLock) {try {if (limit++ % 50 == 0)System.out.println();System.out.print(id + " ");outLock.wait(10);}catch (InterruptedException e) {}}}}}}}Zarz¹dzanie w¹tkamipackage jbPack;import java.awt
[ Pobierz całość w formacie PDF ]
Darmowy hosting zapewnia PRV.PL