Ok ho corretto, ci sono riuscito, era una dimenticanza nel codice!
ho solo un problema con lo stop e il resume del thread... se chiamo myStop() nessun problema, se pero' chiamo myResume() mi da questa eccezione:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at test.MyTimer.myResume(MyTimer.java:43)
at test.MyTestTimer.main(MyTestTimer.java:17)
riposto il codice:
codice:public class MyTestTimer { public static void main(String[] args) { MyTimer t = new MyTimer(1000, 1, 10); t.addMyTimerListener(new MyTimerListener() { public void tic(MyTimerEvent t) { System.out.println("TICCATO"); } }); } } ________________________________________ import java.util.*; public class MyTimer implements Runnable { private int delay, intervallo, puntoFire, contatore = 0; private Vector listener = new Vector(); private boolean flag = false; Thread t; public MyTimer(){ this(1000, 1, 10); } public MyTimer(int delay, int intervallo, int puntoFire){ this.delay = delay; this.intervallo = intervallo; this.puntoFire = puntoFire; t = new Thread(this); t.start(); } public void run() { while(true){ try{ t.sleep(delay); System.out.println(contatore); contatore += intervallo; if(puntoFire == contatore) fireMyTimerEvent(puntoFire); while(flag) wait(); } catch (InterruptedException e){ System.out.println("INTERROTTO"); } } } public void myResume(){ flag = false; notify(); } public void myStop(){ flag = true; } public void addMyTimerListener(MyTimerListener tl){ listener.addElement(tl); } public void removeMyTimerListener(MyTimerListener tl){ listener.removeElement(tl); } public void fireMyTimerEvent(int tic){ MyTimerEvent t = new MyTimerEvent(this, tic); for(int i = 0; i < listener.size(); i++) ((MyTimerListener) listener.elementAt(i)).tic(t); } public void fireMyTimerEvent(String message){ MyTimerEvent t = new MyTimerEvent(this, message); for(int i = 0; i < listener.size(); i++) ((MyTimerListener) listener.elementAt(i)).tic(t); } public int getContatore(){ return contatore; } } ________________________________________ import java.util.EventObject; public class MyTimerEvent extends EventObject{ private int puntoFire; private String message; public MyTimerEvent(Object source){ this(source, 10); } public MyTimerEvent(Object source, int puntoFire){ super(source); this.puntoFire = puntoFire; } public MyTimerEvent(Object source, String message){ super(source); this.message = message; } } ________________________________________ import java.util.EventListener; public interface MyTimerListener extends EventListener{ public void tic(MyTimerEvent te); }

!
Lancill
Rispondi quotando