创建线程的一种方法

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.apache.commons.lang3.StringUtils;

import org.apache.log4j.Logger;

public class RetrieveStatisProxy {


static Logger logger = Logger.getLogger(RetrieveStatisProxy.class);
static Thread doWorkThread = null;
static boolean isRunThread = false;
static Queue<RetrieveResultStatis> QUEUE = null;
static Long TIME_UNIT = 1000l;
static final Object SyncRoot = new Object();
static FaceSystemConfig config = BeanHelper.getBean(FaceSystemConfig.class);


public static void start(){
startThread();
}

static void startThread(){
if(null == doWorkThread){
if(!isRunThread){
isRunThread = true;
doWorkThread = new Thread(doWork);
doWorkThread.setName(RetrieveStatisProxy.class.getSimpleName());
doWorkThread.start();
}
}
}

public static void add(RetrieveResultStatis statis){
synchronized (SyncRoot) {
if(null == QUEUE){
QUEUE = new LinkedList<>();
}
QUEUE.offer(statis);
}
}


static Runnable doWork = new Runnable() {

@Override
public void run() {
logger.debug("--------RetrieveStatisProxy start to work!!!-------");
while(isRunThread){
try{
while(null != QUEUE && QUEUE.size()>0){
RetrieveResultStatis statis = QUEUE.poll();
logger.debug(String.format("------poll from queue:[%s]-----",statis.toString()));
handle(statis);
}
}catch(Exception e){
logger.error(e.getMessage(),e);
}
ThreadHelper.sleepThread(TIME_UNIT*5);
}
}
};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值