我已經對java線程編碼進行了測試,但是我有一些基本問題。經過幾個小時的嘗試和搜索,我決定嘗試一下!無法通知鎖定
我不明白爲什麼我的等待仍然鎖定即使我的通知:
在這裏你可以找到我的代碼:
public class Mymain {
public static void main(String[] args) {
for(int i=0;i<100;i++){
new ThreadClass(i).start();
}
}
}
public class ThreadClass extends Thread {
static boolean ok = false;
int id;
public ThreadClass(int i) {
id = i;
}
public void run() {
System.out.println("Thread start " + id);
Last.toDo(id);
if (id == 5)
try {
waiting();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (id != 5)
awaking();
System.out.println("thread end " + id);
}
private synchronized void awaking() {
// TODO Auto-generated method stub
if (ok) {
System.out.println("i'm " + id + " and i'm Awaking 5");
ok = false;
notify();
System.out.println("I did the notify and i'm " + id);
}
}
private synchronized void waiting() throws InterruptedException {
System.out.println("Sleeping");
ok = true;
wait();
System.out.println("Awake 5");
}
}
然後開始循環,或者去在死鎖不能確定..它應該只是停止與ID = 5的線程,然後下一個線程應重新啓動ID = 5 ..但線程5從未醒來後通知...
在結果中,你可以看到我有2個線程試圖喚醒線程5和螺紋5的自啓動^^
你能告訴我們你的輸出是什麼,它應該是什麼? –
你可以把它放在你的問題中,以便格式化以便於分析嗎? –
@WarrenDew完^^ – JustMe