G
Gast
Gast
Hallo,
ich habe gerade versucht, einen laufenden Thread zu stoppen aber irgendwie funkt das nicht,
kann mir da jemand helfen, danke,
ich habe gerade versucht, einen laufenden Thread zu stoppen aber irgendwie funkt das nicht,
kann mir da jemand helfen, danke,
Code:
Klasse 1:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
aInterface1 = new EthernetInterface("jappos", "localhost", 3100, 20000);
aInterface1.start();
InterfaceTimerTask itt1 = new InterfaceTimerTask(aInterface1, (byte) 1, 1000, 2000);
itt1.startRequest();
aInterface2 = new EthernetInterface("jappos", "localhost", 3100, 20000);
aInterface2.start();
itt2 = new InterfaceTimerTask(aInterface2, (byte) 2, 2000, 2000);
itt2.startRequest();
}
else if (e.getSource() == b1) {
aInterface1.closeInterface();
}
else if (e.getSource() == b2) {
itt2.stopRequest();
}
Klasse 2:
class InterfaceTimerTask extends TimerTask {
private AInterface aInterface;
private Timer timer;
private byte name;
private int time;
private int delay;
public InterfaceTimerTask(AInterface aInterface, byte name, int delay, int time) {
this.aInterface = aInterface;
this.name = name;
this.time = time;
this.delay = delay;
}
public void startRequest() {
timer = new Timer();
timer.scheduleAtFixedRate(this, delay, time);
}
public void stopRequest() { // ich habs mal so probiert, funkt aber leider nicht!!!
synchronized(timer) {
timer.wait(3000);
}
}
@Override
public void run() {
aInterface.writeToRemote(new byte[]{name,3,4,5,6});
}
}