Java:
public class NameOutput extends Thread {
private static int NUMBER_OF_THREAD = 11;
private static int number = 0;
private static Object lock = new Object();
private static int next = 1;
private int myNumber;
public NameOutput() {
}
public void run() {
while (true) {
synchronized (lock) {
number++;
if (number == next) {
System.out.println(getName() + ": " + ": " + next + ": " + number);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
next = (next + 1) % NUMBER_OF_THREAD;
lock.notifyAll();
} else {
try {
number= 0;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
new NameOutput().start();
new NameOutput().start();
new NameOutput().start();
}
ich möchte ein kleines Programm schreiben, so dass jeder Thread seinen Namen jeweils zehnmal hintereinander ausgibt, ohne dass ihm einer der anderen Threads dazwischenfunkt.
Irgendwo gibt es bei mir Deadlock, kann jemand mich bitte unterstützen?
Vielen Dank im Voraus