zoukankan      html  css  js  c++  java
  • 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());
    }
    }
    }

  • 相关阅读:
    20150629_Andriod_06_插入_删除_弹出式操作数据
    20150626_Andriod_02_ListView2_列表与详细信息
    Andriod 字符串数组里加入字符串元素
    20150625_Andriod_02_ListView2_多条目显示_选中
    20150625_Andriod_01_ListView1_条目选中
    Android开发中完全退出程序的三种方法
    Python中的单例模式的几种实现方式的及优化
    jdk与jre
    页面跳转
    用for循环创建对象
  • 原文地址:https://www.cnblogs.com/dieyf/p/13962761.html
Copyright © 2011-2022 走看看