import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author:wangshipeng
* @Date: 2020-11-11
* @Description
*/
public class ThreadUtil {
private ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 10, TimeUnit.DAYS, new LinkedBlockingDeque<>());
private List<Thread1> threadCache = new ArrayList<>();
public static void main(String[] args) {
ThreadUtil threadUtil = new ThreadUtil();
threadUtil.excete();
}
public void excete() {
for (int i = 0; i<10; i++) {
System.out.println(">>>>>>>"+i);
Thread1 thread = new Thread1(i, executor);
threadCache.add(thread);
executor.execute(thread);
}
System.out.println("threadCache>>>>>>>>> "+threadCache);
Thread2 thread2 = new Thread2(threadCache);
Thread thread = new Thread(thread2);
thread.start();
}
private class Thread2 implements Runnable {
private List<Thread1> threadCache;
public Thread2(List<Thread1> threadCache) {
this.threadCache = threadCache;
}
@Override
public void run() {
boolean flag = true;
while (flag) {
// System.out.println("threadCache size >>"+threadCache.size()+" flag>>"+flag);
if (threadCache.size() == 0) {
Thread.currentThread().interrupt();
flag = false;
return;
}
long now = new Date().getTime();
Iterator<Thread1> iterator = threadCache.iterator();
while (iterator.hasNext()) {
Thread1 next = iterator.next();
int num = next.getNum();
//非活体删除
boolean alive = next.isAlive();
if (!alive) {
System.out.println("no alive delete num >>>"+ num);
threadCache.remove(next);
}
//超时删除
boolean isoVertime = next.isoVertime();
if (isoVertime) {
System.out.println("超时中断 >>>>"+num);
next.interrupt(next);
}
}
}
}
}
private class Thread1 implements Runnable {
private ThreadPoolExecutor executor;
private int num;
private Long stime;
private Long etime;
public Thread1(int num, ThreadPoolExecutor executor) {
this.executor = executor;
this.num = num;
}
public boolean isoVertime() {
long now = new Date().getTime();
if (etime == null && stime != null && (now-stime.longValue())/1000 > 10) {
return true;
}
return false;
}
public int getNum() {
return this.num;
}
public boolean isAlive() {
return Thread.currentThread().isAlive();
}
public void interrupt (Thread1 thread1){
stime = null;
System.out.println("isInterrupted>>>>>>>>>>>>>> "+Thread.currentThread());
Thread.currentThread().stop();
}
@Override
public void run() {
System.out.println("num>>"+num+" Thread1>>>>>>>>>"+this);
stime = new Date().getTime();
server();
etime = new Date().getTime();
}
//业务
public void server() {
long id = Thread.currentThread().getId();
System.out.println("thread id >>" + id + " num >>>" + num);
try {
if (num == 2 || num == 3) {
Thread.sleep(60000L);
}
Thread.sleep(3000L);
} catch (Exception e) {
System.out.println("thread error id>>" + id);
}
System.out.println("thread id >>" + id + " num >>>" + num +" end. executor num >>"+executor.getActiveCount());
}
}
}