多线程超时处理的方法_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 多线程超时处理的方法

多线程超时处理的方法

 2014/9/18 18:13:56  ljz0898  程序员俱乐部  我要评论(0)
  • 摘要:packagecn.mytest;importjava.util.ArrayList;importjava.util.HashMap;/***@Description:线程监控超时的工具类*@author*@date2014-9-18下午04:47:12*/publicclassThreadWathcherextendsThread{privatestaticThreadWathcherwatcher;/***存放对应的线程跟开始执行的时间*/privateHashMap<Thread
  • 标签:方法 多线程 超时 线程
package cn.mytest;

import java.util.ArrayList; 
import java.util.HashMap; 
 
 
/**  
* @Description: 线程监控超时的工具类
* @author
* @date 2014-9-18 下午04:47:12
*/
public class ThreadWathcher extends Thread { 
 
 
    private static ThreadWathcher watcher; 
   
    /**
     * 存放对应的线程跟开始执行的时间
     */
    private HashMap<Thread, Long> threadBornTimeCollection;
   
    /**
     * 需要被中断的线程
     */
    private ArrayList<Thread> toRemoveThreads; 
    /**
     * 超时时间
     */
    private long timeOutMills;
    /**
     * 间隔扫描时间
     */
    private long periodMills; 
 
 
    private ThreadWathcher() { 
    /**
    * 设置线程为守护线程 以致主线程停止的时候守护线程也自动终止
    */
        this.setDaemon(true); 
        threadBornTimeCollection = new HashMap<Thread, Long>(); 
        toRemoveThreads = new ArrayList<Thread>(); 
    } 
 
    /**
     * 配置的超时时间必须是间隔检测时间的倍数+3  比如超时时间是1000则 period应该是503
     * @param timeout
     * @param period
     * @return
     */
    public static ThreadWathcher getInstance(long timeout, long period) { 

        if (watcher == null) { 
            watcher = new ThreadWathcher(); 
            watcher.timeOutMills = timeout; 
            watcher.periodMills = period; 
        } 
 
        return watcher; 
    } 
 
 
    public int register(Thread thread) { 
 
        threadBornTimeCollection.put(thread, System.currentTimeMillis()); 

        return threadBornTimeCollection.size(); 
    } 
 
 
    @Override 
    public void run() { 
        super.run(); 
   
        while (true) {// 守护线程 
            try { 
                Thread.sleep(periodMills);// 每隔periodMills秒检查一次 
                for (Thread e : threadBornTimeCollection.keySet()) {// 遍历已经注册过超时处理的线程集合
                    if (Math.abs(threadBornTimeCollection.get(e) 
                            - System.currentTimeMillis()) > timeOutMills 
                            && e.isAlive()) {// 超时 
                        toRemoveThreads.add(e);// 添加到超時线程集合中 
                    } 
                } 
                for (Thread e : toRemoveThreads) {// 遍历超时线程集合 
                    threadBornTimeCollection.remove(e);// 从超时集合中移除 
                    e.interrupt();// 中断超时线程 
                } 
               
 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } finally { 
                if (toRemoveThreads.size() > 0) { 
                    System.out.println("清空超时线程集合"); 
                    toRemoveThreads.clear();// 清空超时线程集合 
                } 
            } 
        } 
    } 









package cn.mytest;

import java.util.ArrayList;
import java.util.HashMap;

public class Main { 
 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        System.out.println("Begin here..."); 
       
        ThreadWathcher wacher = ThreadWathcher.getInstance(1000, 503); 
        wacher.start(); 
        Thread a = new Thread() { 
 
            @Override 
            public void run() { 
                super.run(); 
                int n = 0; 
                try { 
                System.out.println("A线程执行中-----");
                Thread.sleep(1100);
                System.out.println("A线程执行完成.....");
                   /* while (n < 1 && !Thread.interrupted()) { 
                        Thread.sleep(1000); 
                        n++; 
                        System.out.println("In thread a..." + n); 
                    }  */
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block 
                    System.out.println("线程因超时被终止...线程名" + this.getName()); 
                } 
 
 
            } 
 
 
        }; 
        a.setName("a"); 
        wacher.register(a); 
        a.start(); 
        Thread b = new Thread() { 
 
 
            
           
            @Override 
            public void run() { 
                // TODO Auto-generated method stub 
                super.run(); 
                int n = 0; 
                try { 
                System.out.println("B线程执行中-----");
                Thread.sleep(900);
                System.out.println("B线程执行完成.....");
                   /* while (n < 5 && !Thread.interrupted()) { 
 
 
                        Thread.sleep(1000); 
                        n++; 
                        System.out.println("In thread b..." + n); 
                    }  */
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block 
                    System.out.println("线程因超时被终止...线程名" + this.getName()); 
                } 
 
 
            } 
 
 
        }; 
        b.setName("b"); 
        b.start(); 
        wacher.register(b); 
        Thread c = new Thread() { 
 
 
            public void run() { 
                // TODO Auto-generated method stub 
                super.run(); 
                int n = 0; 
                try { 
                System.out.println("C线程执行中-----");
                Thread.sleep(1200);
                System.out.println("C线程执行完成.....");
                   /* while (n < 12 && !Thread.interrupted()) { 
 
 
                        Thread.sleep(1000); 
                        n++; 
                        System.out.println("In thread c..." + n); 
                    }  */
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block 
                    System.out.println("线程因超时被终止...线程名" + this.getName()); 
                } 
 
 
            } 
 
 
        }; 
        c.setName("c"); 
        c.start(); 
        wacher.register(c); 
        Thread d = new Thread() { 
 
 
            
            public void run() { 
                // TODO Auto-generated method stub 
                super.run(); 
                int n = 0; 
                try { 
                System.out.println("D线程执行中-----");
                Thread.sleep(500);
                System.out.println("D线程执行完成.....");
                  /*  while (n < 15 && !Thread.interrupted()) { 
 
 
                        Thread.sleep(1000); 
                        n++; 
                        System.out.println("In thread d..." + n); 
                    }  */
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block 
                    System.out.println("线程因超时被终止...线程名" + this.getName()); 
                } 
 
 
            } 
 
 
        }; 
        d.setName("d"); 
        d.start(); 
        wacher.register(d); 
       
    } 

 

 
 


发表评论
用户名: 匿名