Yun Zhihui (Beijing) Technology Co., Ltd. Chenxin
Scene
A scheduler, two scheduling tasks, respectively, processing the TXT file in two directories, a scheduling task to deal with some complex problems will continue for a particularly long time, and even the possibility of blocking. We need a manager to manage these tasks, and when the last execution time of this task is now more than 5 dispatch cycles, stop the thread and restart it to ensure that there are no pending TXT file stacks in the two target directories.
Problem
Task1 and Task2 are dispatched directly using the Java default thread pool. Due to the various uncontrolled causes of external txt, the TASK2 thread is blocked. The phenomenon is that task1 and the thread pool scheduler are running normally, but Task2 is slow to move.
Of course, it is important to find specific blocking causes and to address them in a targeted way. However, such a measure may not be able to fully, thoroughly and comprehensively deal with all unknown situations. We need to ensure the robustness of the task thread or scheduler!
Programme plan
The thread pool Scheduler does not have a native API for monitoring processing of the business run state of the scheduled thread. Because Task2 is blocked in our business logic, the best way to do this is to write a taskmanager, all of the task threads to this taskmanager to register themselves before performing the task. This taskmanager is responsible for the real-time monitoring of tasks within each of their own jurisdictions!
The next focus is on how to handle tasks with more than 5 execution cycles.
The scheme is as follows:
Once the task thread is found, it is immediately aborted and then restarted again;
Once the task thread is found, the entire pool is emptied and stopped, and re-placed in the two task--"task-clear case";
Programme implementation
Restart after abort
Task Implementation Class
Classfiletask extends Thread {
Private long lastexectime = 0;
protected long interval = 10000;
public long getLastExecTime() { returnlastExecTime;}public void setLastExecTime(longlastExecTime) { this.lastExecTime =lastExecTime;}public long getInterval() { return interval;}public void setInterval(long interval) { this.interval = interval;}public File[] getFiles() { return null;}@Overridepublic void run() { while(!Thread.currentThread().isInterrupted()) { lastExecTime = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName() + " is running ->" + new Date()); try { Thread.sleep(getInterval() * 6 * 1000); } catch(InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); // 当线程池shutdown之后,这里就会抛出exception了 } }}
}
TaskManager
public class TaskManager implements Runnable {
Private final static Log logger = Logfactory.getlog (Taskmanager.class);
public Set<FileTask> runners = newCopyOnWriteArraySet<FileTask>();ExecutorService pool =Executors.newCachedThreadPool();
Public voidregistercoderunnable (Filetask process) {
Runners.add (process);
}
Publictaskmanager (set<filetask>runners) {this.runners = runners;} @Overridepublic void Run () {while (! Thread.CurrentThread (). isinterrupted ()) {try {long current = System.currenttimemillis (); for (Filetask wrapper:runners) {if (Current-wrapper.getlastexectime () >wrapper.getinterval () * 5) { Wrapper.interrupt (); For (File file:wrapper.getFiles ()) {file.delete (); } wrapper.start (); }}} catch (Exception E1) {logger.error ("error happens when we trying to interrupt and Restar T a task "); Exceptioncollector.registerexception (E1); } try {Thread.Sleep (500); } catch (Interruptedexception e) {}}}
}
This code will error Java.lang.Thread illegalthreadstateexception. Why is it? In fact this is a very basic problem, you should not be as careless as I am. Check out the notes for Thread.Start () for a paragraph like this:
It's never legal to start a, thread more thanonce. In particular, a thread is not being restarted once it has completedexecution.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java using the default thread pool trampled pits (i)