1 16 17 package org.apache.tomcat.util.threads; 18 19 20 28 public class Reaper extends Thread { 29 30 31 private static org.apache.commons.logging.Log log= 32 org.apache.commons.logging.LogFactory.getLog(Reaper.class ); 33 34 private boolean daemon = false; 35 36 public Reaper() { 37 if (daemon) 38 this.setDaemon(true); 39 this.setName("TomcatReaper"); 40 } 41 42 public Reaper(String name) { 43 if (daemon) 44 this.setDaemon(true); 45 this.setName(name); 46 } 47 48 private long interval = 1000 * 60; 50 54 ThreadPoolRunnable cbacks[] = new ThreadPoolRunnable[30]; Object tdata[][] = new Object [30][]; int count = 0; 57 58 60 Object lock = new Object (); 61 static boolean running = true; 62 63 65 public void setDefaultInterval(long t) { 66 interval = t; 67 } 68 69 public long getDefaultIntervale() { 70 return interval; 71 } 72 73 public int addCallback(ThreadPoolRunnable c, int interval) { 74 synchronized (lock) { 75 cbacks[count] = c; 76 count++; 77 return count - 1; 78 } 79 } 80 81 public void removeCallback(int idx) { 82 synchronized (lock) { 83 count--; 84 cbacks[idx] = cbacks[count]; 85 cbacks[count] = null; 86 } 87 } 88 89 public void startReaper() { 90 running = true; 91 this.start(); 92 } 93 94 public synchronized void stopReaper() { 95 running = false; 96 if (log.isDebugEnabled()) 97 log.debug("Stop reaper "); 98 this.interrupt(); } 100 101 public void run() { 102 while (running) { 103 if (!running) 104 break; 105 try { 106 Thread.sleep(interval); 107 } catch (InterruptedException ie) { 108 } 110 111 if (!running) 112 break; 113 for (int i = 0; i < count; i++) { 114 ThreadPoolRunnable callB = cbacks[i]; 115 if (callB != null) { 118 callB.runIt(tdata[i]); 119 } 120 if (!running) 121 break; 122 } 123 } 124 } 125 } 126 | Popular Tags |