मैं दो धागे एक के बाद एक चलाना चाहते हैं, सो() या ताले का उपयोग किए बिना, लेकिन एक गतिरोध होता है! मेरे कोड में क्या गलत है? मैंने प्रतीक्षा() और NOTifyAll() और ऑब्जेक्ट ऑब्जेक्ट का उपयोग किया।जावा इंतजार और सूचित करता है गतिरोध
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) {
}
}
}
}
}
मैं इन सवालों को समझने कभी नहीं। यदि आप अनुक्रमिक निष्पादन चाहते हैं, तो आप धागे का उपयोग क्यों कर रहे हैं? – EJP
ईजेपी को कभी भी ध्यान न दें! बस अभ्यास! – John