私は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