• aaaaaaaaaaaaaaa



    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());
    }
    }
    }

  • 相关阅读:
    CentOS6.5 安装 tomcat
    CentOS6.5 安装 jdk1.7
    CentOS6.5 mysql 5.5安装
    java使用split切割字符串的时候,注意转义字符
    hibernate中设置BigDeCimal的精度
    powerdesinger中建立一个表后,出现Existence of index的警告
    MySQL数据库一个字段对应多个值得模糊查询
    系统收到了多个不同的 Content-Disposition 标头。为了避免遭到 HTTP 响应拆分攻击,这种情况是不允许的。
    下载的文件名字当前时间(精确到秒)+ 随机数
    简单的poi导出excel文件
  • 原文地址:https://www.cnblogs.com/dieyf/p/13962761.html
Copyright © 2020-2023  润新知