Coverage Report - com.liquidatom.derbyscore.domain.ClockExecutor
 
Classes in this File Line Coverage Branch Coverage Complexity
ClockExecutor
0%
0/39
0%
0/12
3
 
 1  
 package com.liquidatom.derbyscore.domain;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 import javax.annotation.concurrent.GuardedBy;
 6  
 import javax.annotation.concurrent.ThreadSafe;
 7  
 import org.slf4j.Logger;
 8  
 import org.slf4j.LoggerFactory;
 9  
 
 10  
 /**
 11  
  *
 12  
  * @author Russell Francis (russ@metro-six.com)
 13  
  */
 14  
 @ThreadSafe
 15  0
 public class ClockExecutor implements Runnable {
 16  
 
 17  0
     static private final Logger log = LoggerFactory.getLogger(ClockExecutor.class);
 18  
     static private final long FREQUENCY = 500;  // delay in milliseconds between updates of the clock.
 19  
 
 20  0
     @GuardedBy("itself")
 21  
     private final List<Clock> clocks = new ArrayList<Clock>();
 22  
 
 23  
     public void run() {
 24  
         try {
 25  0
             final List<Clock> removeClockList = new ArrayList<Clock>();
 26  
             while (true) {
 27  0
                 synchronized (clocks) {
 28  
                     try {
 29  0
                         long currentTimeMillis = System.currentTimeMillis();
 30  0
                         for (Clock clock : clocks) {
 31  0
                             clock.tick(currentTimeMillis);
 32  
 
 33  0
                             if (clock.isTerminated()) {
 34  0
                                 removeClockList.add(clock);
 35  
                             }
 36  
                         }
 37  
                     }
 38  
                     finally {
 39  0
                         clocks.removeAll(removeClockList);
 40  0
                         removeClockList.clear();
 41  0
                     }
 42  0
                 }
 43  
 
 44  
                 // yield to other thread allow them to do some work.
 45  
                 try {
 46  0
                     Thread.sleep(FREQUENCY);
 47  
                 }
 48  0
                 catch (InterruptedException e) {
 49  0
                     if (log.isWarnEnabled()) {
 50  0
                         log.warn("Clock interupted exiting clock thread: " + e.getMessage());
 51  
                     }
 52  0
                     Thread.currentThread().interrupt();
 53  0
                     break;
 54  0
                 }
 55  
             }
 56  
         }
 57  
         finally {
 58  0
             fireOnTerminate();
 59  0
         }
 60  0
     }
 61  
 
 62  
     protected void fireOnTerminate() {
 63  0
         synchronized (clocks) {
 64  0
             for (Clock clock : clocks) {
 65  0
                 clock.fireOnTerminate();
 66  
             }
 67  0
         }
 68  0
     }
 69  
 
 70  
     public void addClock(final Clock clock) {
 71  0
         if (clock != null) {
 72  0
             synchronized (clocks) {
 73  0
                 clocks.add(clock);
 74  0
             }
 75  
         }
 76  0
     }
 77  
 
 78  
     public void removeClock(final Clock clock) {
 79  0
         if (clock != null) {
 80  0
             synchronized (clocks) {
 81  0
                 clocks.remove(clock);
 82  0
             }
 83  
         }
 84  0
     }
 85  
 }