java常用組件--線程池

/*
* 創建日期 2005-12-7
*/
package cn.com.skyon.sms.awork;

import org.apache.log4j.Logger;

import cn.com.skyon.sms.Other.DataContainer;
/**
* 線程池中的執行者
* @author ghl
*/
public class Peon implements Runnable
{
private static Logger logger = Logger.getLogger(Peon.class);
private DataContainer container = DataContainer.getInstance();
public void run()
{
while(!Thread.currentThread().isInterrupted())
{
try
{
Runnable runnable ;
synchronized (container)
{
try
{
while (container.executorSize() == 0 && !Thread.currentThread().isInterrupted())
{
try
{
container.wait();
}
catch (InterruptedException e)
{
}
}
runnable = container.popExecutor();
}
finally
{
container.notifyAll();
}
}
if (runnable != null)
{
logger.info("成功獲得執行器,開始執行……");
runnable.run();
logger.info("執行完畢");
}
}
catch (Exception e)
{
logger.error("執行期間出現錯誤",e);
}
}
logger.info("執行線程結束");
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章