Chcę uruchomić dwa wątki jeden po drugim, bez użycia trybu uśpienia() lub blokady, ale dzieje się zakleszczenie! Co jest nie tak z moim kodem? Użyłem wait() i notifyAll() i obiektu Object.Zatrzymanie Java i powiadamianie sprawia, że zakleszczenie
public class Test {
public static void main(String[] args) throws InterruptedException {
PrintChar a = new PrintChar('a');
PrintChar b = new PrintChar('b');
Thread ta = new Thread(a);
Thread tb = new Thread(b);
ta.start();
tb.start();
}
}
class PrintChar implements Runnable {
final Object o = new Object();
char ch;
public PrintChar(char a) {
ch = a;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (o) {
System.out.print(ch);
try {
o.wait();
o.notifyAll();
} catch (InterruptedException ex) {
}
}
}
}
}
nie rozumiem te pytania. Jeśli chcesz sekwencyjne wykonanie, dlaczego używasz wątków? – EJP
Mniejsza o EJP! Po prostu ćwiczyć! – John