Threads Watchdog

bluebaby

Aktives Mitglied
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();
	}
}
 

Flown

Administrator
Mitarbeiter
IMHO sind Locks sauberer als die Objectmethoden (seit Java 5 - steht aber auch im Tutorial).

Threads höflich beenden findest du hier: TUTORIAL

Ich hab nur dein Beispiel aufgeräumt und Threads höflich beendet (Achtung: Java 8):

Java:
public class PingPongsyntest {
  public static void main(String[] args) {

    PingPongsyn p1 = new PingPongsyn("Ping");
    PingPongsyn p2 = new PingPongsyn("Pong");

    new WatchDogsyn(p1, 2000);
    new WatchDogsyn(p2, 2000);
  }
}


Java:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PingPongsyn implements Runnable {

  private static final Lock LOCK = new ReentrantLock();
  private static final Condition CONDITION = LOCK.newCondition();

  private String name;

  public PingPongsyn(String name) {
    this.name = name;
  }

  @Override
  public void run() {
    while (!Thread.currentThread().isInterrupted()) {
      LOCK.lock();
      System.out.println(name);
      CONDITION.signalAll();
      try {
        CONDITION.await();
      }
      catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
      finally {
        LOCK.unlock();
      }
    }
  }
}


Java:
import java.util.concurrent.TimeUnit;

public class WatchDogsyn {

  public static final int STANDARD_DURATION = 500; // milliseconds
  private Runnable r;
  private int duration; // milliseconds

  public WatchDogsyn(Runnable r) {
    this(r, STANDARD_DURATION);
  }

  public WatchDogsyn(Runnable r, int duration) {
    this.r = r;
    this.duration = duration;
    watch();
  }

  public void watch() {
    new Thread(() -> {
      Thread t = new Thread(r);
      t.start();
      try {
        TimeUnit.MILLISECONDS.sleep(duration);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      t.interrupt();
    }).start();
  }
}
 

Neue Themen


Oben