在 Runnable 中停止计时器任务



我已经卡住了,不知道如何在我的程序中停止 2 个计时器。我有一个线程,它使用套接字初始化连接,然后我有两个 Runnable,一个用于输入流,一个用于输出流 这是我的代码:

public class Receiver extends Thread {
public void run() {
        initializeThreads();
    }
public static void initializeThreads() {
        try {
            // create new thread pool with four threads
            application = Executors.newCachedThreadPool();
            // create ArrayBlockQueueSync to store ints
            Buffer sharedLocation = new ArrayBlockQueueSync();
            // execute tasks
            application.submit(new Reader(client.getInputStream(), sharedLocation));
            application.submit(new Writer(client.getOutputStream(), sharedLocation));
            application.submit(new MyTimer1(sharedLocation));
            application.submit(new MyTimer2(sharedLocation));
        } catch (IOException e) {
            WriteExceptions.writeToFile(e);
            System.err.println("Error on the run Receiver.n" + e);
        }
    }
    public static void disconnect() {
        try {
            client.close();
            System.out.println("Close Socket");
            shutdownAndAwaitTermination();
        } catch (IOException e) {
            e.printStackTrace();
            WriteExceptions.writeToFile(e);
        }
    }
    // This I found on the javadoc
    public static void shutdownAndAwaitTermination() {
        application.shutdown(); // Disable new tasks from being submitted
        try {
            // Wait a while for existing tasks to terminate
            if (!application.awaitTermination(60, TimeUnit.SECONDS)) {
                application.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!application.awaitTermination(60, TimeUnit.SECONDS))
                    System.err.println("Pool did not terminate");
            }
        } catch (InterruptedException ie) {
            WriteExceptions.writeToFile(ie);
            // (Re-)Cancel if current thread also interrupted
            application.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }
}

作者和读者如下:

public class Writer implements Runnable {
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                // read or write the socket
            }
        }
}

计时器是这样的:

public class Timer1 implements Runnable {
    @Override
    public void run() {
        try {
            // Declare the timer
            Timer t = new Timer();
            // Set the schedule function and rate
            t.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    // perform my task
                    } catch (InterruptedException e) {
                        WriteExceptions.writeToFile(e);
                    }
                }
            },5 * 60 * 1000,24 * 60 * 60 * 1000);

        } catch (Exception ex) {
            WriteExceptions.writeToFile(ex);
        }
    }
}

我的问题,当我调用接收器的断开连接方法时,计时器没有停止。谢谢

多亏了@fge建议,我使用ScheduledExecutorService类并且可以正常工作。

最新更新