Code example for ScheduledThreadPoolExecutor

Methods: remove, schedule

0
    	super.onDestroy(); 
    } 
     
    private void initTimer(int timeoutInSeconds) {
        if (executor == null) {
            executor = new ScheduledThreadPoolExecutor(1);
        } 
		 
        checker = new LogoutChecker(executor, timeoutInSeconds, TimeUnit.SECONDS);
        checker.reset();
        checker.reschedule(TimeUnit.SECONDS.toMillis(timeoutInSeconds));
		 
        if (DEBUG) Log.d(LOG_TAG, "checker initialized, callback: "+callback);
    } 
     
    private void resetTimer() { 
		if (DEBUG) Log.d(LOG_TAG, "reset called");
    	checker.reset();
    } 
     
    private void logout() { 
		if (DEBUG) Log.d(LOG_TAG, "logout called");
    	sendJavascript(callback);
    } 
 
    private class LogoutChecker implements Runnable {
		 
    	private final ScheduledThreadPoolExecutor executor;
    	private final long timeOutInMillis;
    	 
    	private long nextLogoutInMillis;
    	 
		public LogoutChecker(ScheduledThreadPoolExecutor executor, long timeOut, TimeUnit timeUnit) {
			this.executor = executor;
			this.timeOutInMillis = timeUnit.toMillis(timeOut);
		} 
 
		public synchronized void reset() { 
			final long now = System.currentTimeMillis();
			this.nextLogoutInMillis = now + timeOutInMillis;
		} 
		 
		public synchronized void reschedule(long whenInMillis) {
			executor.remove(LogoutChecker.this);
			executor.schedule(LogoutChecker.this, whenInMillis, TimeUnit.MILLISECONDS);
			if (DEBUG) Log.d(LOG_TAG, "timer (re)scheduled at "+whenInMillis);
		} 
 
		public synchronized void run() { 
			if (DEBUG) Log.d(LOG_TAG, "checking logout ...");