Hallo, ich hab da mal eine Frage und zwar wie schaff ich es das der Watchdog den Thread beendet? Also so dass das Ping Pong nach der angegebenen Zeit stopt.
Java:
public class PingPongsyn extends Thread {
boolean isPing=false;
Object lock;
String name;
String s="/n";
public PingPongsyn(Object lock, boolean isPing,String name) {
this.lock = lock;
this.isPing = isPing;
this.name=name;
}
public PingPongsyn(String name,boolean isPing, Object lock){
this.name=name;
this.isPing=isPing;
this.lock=lock;
}
public void isPing(){
this.isPing=true;
}
public void isnotPing(){
this.isPing=false;
}
public void pingPong()throws Exception{
synchronized(lock){
while(true){
if (isPing){
System.out.println(this.name);
lock.notifyAll();
lock.wait();
}else{
}
}
}
}
public void run() {
try {
pingPong();
} catch (Exception e) {
}
}
}
Java:
public class WatchDogsyn extends Thread {
public static final int DAUER=500; //Dauer in msec
private PingPongsyn t;
private int dauer; //Dauer in msec
Object z;
/**
* Standard-Konstruktur
* @param t Thread, der abgebrochen werden soll
*/
public WatchDogsyn(PingPongsyn t) {
this(t,DAUER);
}
/**
* Konstruktur mit Angabe der Ausführungsdauer
* @param t Thread, der abgebrochen werden soll
* @param dauer Länge der Ausführung in msec
*/
public WatchDogsyn(PingPongsyn t, int dauer) {
this.t=t;
this.dauer=dauer;
}
/**
* Threadinhalt: Abbruch des referenzierten Threads
*/
public void run() {
try {
sleep(dauer);
t.isPing();
} catch (InterruptedException e) {
}
}
}
Java:
public class PingPongsyntest {
public static void main(String[] args) {
Object o = new Object();
PingPongsyn p1=new PingPongsyn("Ping",true,o);
PingPongsyn p2=new PingPongsyn("Pong",true,o);
p1.start();
p2.start();
new WatchDogsyn(p1,2000).start();
new WatchDogsyn(p2,2000).start();
// (new PingPongsyn(o, true,"Ping")).start();
// (new PingPongsyn(o, true,"Pong")).start();
// new WatchDogsyn(o,5000).start();
}
}