fix thead
parent
f79437f32b
commit
503217bc43
|
|
@ -8,9 +8,9 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ThreadManager {
|
||||
|
|
@ -26,21 +26,25 @@ public class ThreadManager {
|
|||
private static ItemLogTask itemLogTask;
|
||||
public static int SLEEP_INTEVAL_TIME = 60; //每1分钟检查一次
|
||||
|
||||
private static Set<Future<?>> scheduledFutureSets = new HashSet<>();
|
||||
|
||||
private static volatile ScheduledThreadPoolExecutor scheduledExecutor; //管理task
|
||||
|
||||
public void init() {
|
||||
scheduledExecutor = new ScheduledThreadPoolExecutor(16, new ScheduleThreadFactory());
|
||||
}
|
||||
|
||||
public void init(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
|
||||
platConfigureTask = configurableApplicationContext.getBean(PlatConfigureTask.class);
|
||||
mongoDataHandlerTask = configurableApplicationContext.getBean(MongoDataHandlerTask.class);
|
||||
dataReportTask = configurableApplicationContext.getBean(DataReportTask.class);
|
||||
itemLogTask = configurableApplicationContext.getBean(ItemLogTask.class);
|
||||
// DataReportTask dataReportTask = new DataReportTask();
|
||||
// dataReportTask.start();
|
||||
new RetrySendIndicationThread("retry-indi").start();
|
||||
mongoDataHandlerTask.start();
|
||||
dataReportTask.start();
|
||||
itemLogTask.start();
|
||||
|
||||
scheduledExecutor = new ScheduledThreadPoolExecutor(8, new ScheduleThreadFactory());
|
||||
scheduledExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
|
||||
scheduledExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
|
||||
go();
|
||||
|
||||
}
|
||||
|
||||
private ThreadManager(){}
|
||||
|
|
@ -52,18 +56,20 @@ public class ThreadManager {
|
|||
return instance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static class ScheduleThreadFactory implements ThreadFactory {
|
||||
|
||||
final ThreadGroup group;
|
||||
|
||||
final AtomicInteger threadNumber = new AtomicInteger(1);
|
||||
static AtomicInteger threadNumber = new AtomicInteger(1);
|
||||
|
||||
final String namePrefix;
|
||||
|
||||
ScheduleThreadFactory() {
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
||||
namePrefix = "ScheduleThreadPool" + "-thread-";
|
||||
namePrefix = "ScheduleThreadPool-test" + "-thread-";
|
||||
}
|
||||
|
||||
public Thread newThread(Runnable r) {
|
||||
|
|
@ -79,16 +85,14 @@ public class ThreadManager {
|
|||
|
||||
public static void go() {
|
||||
int now = (int)(System.currentTimeMillis()/1000);
|
||||
scheduledExecutor = new ScheduledThreadPoolExecutor(16, new ScheduleThreadFactory());
|
||||
|
||||
// testTask 已秒为单位 延迟10s, 间隔30s为周期执行
|
||||
int delayForMinute = 60 - (now%60);
|
||||
LOGGER.info("delayForMinute---->>>>{}" ,delayForMinute);
|
||||
scheduledExecutor.scheduleAtFixedRate(platConfigureTask, 10, SLEEP_INTEVAL_TIME, TimeUnit.SECONDS);
|
||||
scheduledExecutor.scheduleAtFixedRate(new MinuteTask(), delayForMinute, 60, TimeUnit.SECONDS);
|
||||
scheduledExecutor.scheduleAtFixedRate(new SecondsTask(), 0, 1, TimeUnit.SECONDS);
|
||||
scheduledExecutor.scheduleAtFixedRate(new FetchBloodyEndDataThread(), 0, 1, TimeUnit.SECONDS);
|
||||
scheduledExecutor.scheduleAtFixedRate(new Thread(){
|
||||
scheduledFutureSets.add(scheduledExecutor.scheduleAtFixedRate(platConfigureTask, 10, SLEEP_INTEVAL_TIME, TimeUnit.SECONDS));
|
||||
scheduledFutureSets.add( scheduledExecutor.scheduleAtFixedRate(new MinuteTask(), delayForMinute, 60, TimeUnit.SECONDS));
|
||||
scheduledFutureSets.add( scheduledExecutor.scheduleAtFixedRate(new SecondsTask(), 0, 1, TimeUnit.SECONDS));
|
||||
scheduledFutureSets.add( scheduledExecutor.scheduleAtFixedRate(new FetchBloodyEndDataThread(), 0, 1, TimeUnit.SECONDS));
|
||||
scheduledFutureSets.add( scheduledExecutor.scheduleAtFixedRate(new Thread(){
|
||||
public void run () {
|
||||
long lastMilis = System.currentTimeMillis();
|
||||
try { Thread.sleep(1000L); } catch (InterruptedException e) { }
|
||||
|
|
@ -97,11 +101,7 @@ public class ThreadManager {
|
|||
reStart();
|
||||
}
|
||||
}
|
||||
}, delayForMinute, 1, TimeUnit.SECONDS);
|
||||
scheduledExecutor.execute(new RetrySendIndicationThread("retry_send"));
|
||||
scheduledExecutor.execute(dataReportTask);
|
||||
scheduledExecutor.execute(mongoDataHandlerTask);
|
||||
scheduledExecutor.execute(itemLogTask);
|
||||
}, delayForMinute, 1, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
public static ScheduledThreadPoolExecutor getScheduledExecutor() {
|
||||
|
|
@ -110,7 +110,11 @@ public class ThreadManager {
|
|||
|
||||
public static void reStart(){
|
||||
LOGGER.info("重启线程池!!");
|
||||
scheduledExecutor.shutdown();
|
||||
// scheduledExecutor.shutdown();
|
||||
for(Future future : scheduledFutureSets){
|
||||
future.cancel(false);
|
||||
}
|
||||
scheduledFutureSets.clear();
|
||||
go();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
@Component
|
||||
public class ItemLogTask extends Thread {
|
||||
public ItemLogTask(){
|
||||
super("itemLog");
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){
|
||||
|
|
|
|||
Loading…
Reference in New Issue