Scheduled java timer tasks

What happens in the following case?

Timer t = new Timer();
t.schedule(...);
t = new Timer();

In particular, what happens to the tasks that I scheduled on Timer t after I assigned a new instance of Timer to t?

+5
source share
5 answers

They do not go away. Each object Timeris associated with a background process. Even when you delete all the links to yours Timerin your program, the background process will continue to work (it contains its own link to the object). Because of this, the object will not be affected by garbage collection.

See official documentation for more details .

Timer - , ... , Timer , ​​ , ( ). .

+7

. , ( )

+2

API- , . , . , , . :

" , Timer , ​​ , ( ). . -, . , ."

0

private ScheduledExecutorService scheduler;
private AccurateScheduledRunnable periodic;
private ScheduledFuture<?> periodicMonitor;
private int taskPeriod = 30;
private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
private SimpleDateFormat sdfHour = new SimpleDateFormat("HH");

. , .

  scheduler = Executors.newSingleThreadScheduledExecutor();   
            periodic = new AccurateScheduledRunnable() {

                private final int ALLOWED_TARDINESS = 200;
                private int countRun = 0;
                private int countCalled = 0;

                @Override
                public void run() {
                    countCalled++;
                    if (this.getExecutionTime() < ALLOWED_TARDINESS) {
                        countRun++;
                        dateNext = new java.util.Date();
                        dateLast = new java.util.Date();
                        long tme = dateNext.getTime();
                        tme += (taskPeriod * 60) * 1000;
                        dateNext.setTime(tme);
                        //System.out.println("");
                        //System.out.println("");
                        //System.out.println("Next Sheduled Time at : " + sdf.format(dateNext));
                        //System.out.println("Periodic Cycle In : " + (countRun) + "/" + countCalled + " at " + sdf.format(dateLast));
                        //ti.displayMessage(null, " Running Sheduled Task at " + sdf.format(new Date()), TrayIcon.MessageType.NONE);
                        distAppInfo();
                    }
                }
            };
            periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.MINUTES);
            periodic.setThreadMonitor(periodicMonitor);

, f.e.

long she = periodicMonitor.getDelay(TimeUnit.SECONDS);

abstract class AccurateScheduledRunnable implements Runnable {

    private ScheduledFuture<?> thisThreadsMonitor;

    public void setThreadMonitor(ScheduledFuture<?> monitor) {
        this.thisThreadsMonitor = monitor;
    }

    protected long getExecutionTime() {
        long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS);
        return delay;
    }
}
0

This will depend on which Timer.schedule (..) method you plan to use. If the timer is set to run again, assigning a new instance of timer t will not result in garbage collection, as the timer thread will remain active. If you set a timer to run once, then the object will receive garbage collection. at least what the documentation says ..

0
source

All Articles