1 31 32 package org.opencms.scheduler; 33 34 import org.opencms.main.CmsLog; 35 36 import org.apache.commons.logging.Log; 37 38 47 public class CmsSchedulerThread extends Thread { 48 49 50 private static final Log LOG = CmsLog.getLog(CmsSchedulerThread.class); 51 52 53 private CmsSchedulerThreadPool m_pool; 54 55 56 private boolean m_run; 57 58 59 private Runnable m_runnable; 60 61 71 CmsSchedulerThread( 72 CmsSchedulerThreadPool pool, 73 ThreadGroup threadGroup, 74 String threadName, 75 int prio, 76 boolean isDaemon) { 77 78 this(pool, threadGroup, threadName, prio, isDaemon, null); 79 } 80 81 91 CmsSchedulerThread( 92 CmsSchedulerThreadPool pool, 93 ThreadGroup threadGroup, 94 String threadName, 95 int prio, 96 boolean isDaemon, 97 Runnable runnable) { 98 99 super(threadGroup, threadName); 100 m_run = true; 101 m_pool = pool; 102 m_runnable = runnable; 103 setPriority(prio); 104 setDaemon(isDaemon); 105 start(); 106 } 107 108 111 public void run() { 112 113 boolean runOnce = (m_runnable != null); 114 115 while (m_run) { 116 setPriority(m_pool.getThreadPriority()); 117 try { 118 if (m_runnable == null) { 119 m_runnable = m_pool.getNextRunnable(); 120 } 121 122 if (m_runnable != null) { 123 m_runnable.run(); 124 } 125 } catch (InterruptedException e) { 126 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_INTERRUPTED_1, getName()), e); 127 } catch (Throwable t) { 128 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_ERROR_1, getName()), t); 129 } finally { 130 if (runOnce) { 131 m_run = false; 132 } 133 m_runnable = null; 134 } 135 } 136 if (LOG.isDebugEnabled()) { 137 LOG.debug(Messages.get().getBundle().key(Messages.LOG_THREAD_SHUTDOWN_1, getName())); 138 } 139 } 140 141 144 void shutdown() { 145 146 m_run = false; 147 } 148 } | Popular Tags |