J'ai un test sur le codage de thread java mais j'ai un problème de base .. après des heures d'essai et de recherche, j'ai décidé d'essayer là!Impossible de relacher le verrou avec notifier
Je ne comprends pas pourquoi mon attente est toujours verrouillé même après mon Notify:
Ici vous pouvez trouver mon code:
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");
}
}
Ensuite, il commence en boucle ou il va dans le verrou mort pas sûr .. il devrait juste arrêter le fil avec id = 5 et le prochain thread devrait redémarrer l'id = 5 .. mais le fil 5 ne se réveillent jamais après le notifier ...
Dans le résultat que vous pouvez voir que j'ai eu 2 fils essayant de se réveiller le fil 5 et le fil 5 est toujours en attente depuis le début ^^
Pouvez-vous nous montrer quelle est votre production et ce qu'elle devrait être? –
Pouvez-vous mettre cela dans votre question afin qu'il puisse être formaté pour une analyse facile? –
@WarrenDew Terminé ^^ – JustMe