Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.lastaflute.di.helper.timer;
 
 import java.util.List;

Author(s):
modified by jflute (originated in Seasar)
 
 public class TimeoutManager implements Runnable {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     protected static final TimeoutManager instance = new TimeoutManager();
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected Thread thread;
     protected final TimeoutLinkedList timeoutTaskList = new TimeoutLinkedList();
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     private TimeoutManager() {
     }
 
     public static TimeoutManager getInstance() {
         return ;
     }
 
     // ===================================================================================
     //                                                                          Management
     //                                                                          ==========
     public synchronized void start() {
         if ( == null) {
              = new Thread(this"Lasta_Di-TimeoutManager");
             .setDaemon(true);
             .start();
         }
     }
 
     public synchronized void stop() {
         if ( != null) {
             .interrupt();
              = null;
         }
     }
 
     public boolean stop(long timeoutMillisthrows InterruptedException {
         Thread thread = this.;
         synchronized (this) {
             if (thread == null) {
                 return true;
             }
             this. = null;
         }
         thread.interrupt();
         thread.join(timeoutMillis);
         return !thread.isAlive();
     }
 
     public synchronized void clear() {
         .clear();
     }
 
     public synchronized TimeoutTask addTimeoutTarget(final TimeoutTarget timeoutTargetfinal int timeoutfinal boolean permanent) {
         final TimeoutTask task = new TimeoutTask(timeoutTargettimeoutpermanent);
         .addLast(task);
         if (.size() == 1) {
             start();
         }
         return task;
     }
 
     public synchronized int getTimeoutTaskCount() {
         return .size();
     }
 
     public void run() {
         boolean interrupted = false;
         for (;;) {
             final List<TimeoutTaskexpiredTask = getExpiredTask();
            for (final Iterator<TimeoutTaskit = expiredTask.iterator(); it.hasNext();) {
                final TimeoutTask task = (TimeoutTaskit.next();
                task.expired();
                if (task.isPermanent()) {
                    task.restart();
                }
            }
            if (interrupted || .isInterrupted() || stopIfLeisure()) {
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (final InterruptedException e) {
                interrupted = true;
            }
        }
    }
    protected synchronized List<TimeoutTaskgetExpiredTask() {
        final List<TimeoutTaskexpiredTask = new ArrayList<TimeoutTask>();
        try {
            if ( == null || .isEmpty()) {
                return expiredTask;
            }
        } catch (NullPointerException e) {
            return expiredTask;
        }
        for (TimeoutLinkedList.Entry e = .getFirstEntry(); e != nulle = e.getNext()) {
            final TimeoutTask task = (TimeoutTaske.getElement();
            if (task.isCanceled()) {
                e.remove();
                continue;
            }
            if (task.isStopped()) {
                continue;
            }
            if (task.isExpired()) {
                expiredTask.add(task);
                if (!task.isPermanent()) {
                    e.remove();
                }
            }
        }
        return expiredTask;
    }
    protected synchronized boolean stopIfLeisure() {
        try {
            if ( == null || .isEmpty()) {
                 = null;
                return true;
            }
        } catch (NullPointerException e) {
            return true;
        }
        return false;
    }
New to GrepCode? Check out our FAQ X