How to cancel an already scheduled TimerTask?

I have a tiny problem that I can't seem to do right. I have the following class in java:

package pooledtcpconnector.utilities;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class Notifier implements Runnable {

    private final ILogger logger;

    private Timer mTimer;
    private final int Treshold;
    private final InputStream ResponseStream;
    private final TimerTask DoWaitTask;

    public Notifier(final InputStream _ResponseStream, final Integer _Treshold, final ILogger logger) {
        this.logger = logger;

        mTimer = new Timer();

        this.ResponseStream = _ResponseStream;
        this.Treshold = _Treshold;

        DoWaitTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    int mSize = ResponseStream.available();
                    if (mSize >= Treshold) {
                        mTimer.cancel();
                    }
                } catch (final IOException ex) {
                    final String ExceptionMessage = ex.getMessage();
                    logger.LogMessage(
                            this.getClass().getCanonicalName(),
                            "Notifier.DoWaitTask:run.ResponseStream.available",
                            ex.getClass().getCanonicalName(),
                            new String[]{
                                ExceptionMessage,
                                "Parameters:",
                                String.format("-"),
                            });

                    Logger.getLogger(Notifier.class.getCanonicalName()).log(Level.FINE, ex.getMessage(), ex.getCause());
                }
            }
        };
    }

    @Override
    public void run() {
        synchronized (this) {
            mTimer.scheduleAtFixedRate(DoWaitTask, 250, 200);
            // Notification mechanism
            notify();
        }
    }

}

This class would ensure that our application won't start processing the SocketInputStream unless the available method returns at least Treshold. The problem however is that, once I schedule the DoWaitTask with the Timer it runs for eternity. By cancelling the timer the task still runs and the whole application hangs, but more importantly it tries to call available on the stream once it already has been processed and closed. Of course this results in a nice IOException: stream closed.

How could I stop the scheduled task along with the timer? timer.cancel obviously isn't enough.

Regards, Joey


Use TimerTask.cancel() from within your timer task's run() method. According to the Javadoc for this method:

Note that calling this method from within the run method of a repeating timer task absolutely guarantees that the timer task will not run again.


private Timer reportTimer = null;

    if (reportTimer != null) {
        reportTimer.cancel();
        reportTimer = null;
    }

    reportTimer = new Timer();
    reportTimer.schedule(new TimerTask() {}
链接地址: http://www.djcxy.com/p/74908.html

上一篇: 停止计划任务并重新计划它

下一篇: 如何取消已安排的TimerTask?