java监控函数执行时间

java监控函数执行时间

http://blog.csdn.net/ycg01/article/details/1467542

 

 

java监控函数执行时间

标签: javathreadclassstringnull
 分类:
 

由于一些特殊需要,对执行流加上超时控制.以自己的水平想了下面的方法.也许有更好的通用方法,只是我不知道.呵,写出来,供有需要的人参考.

TimeoutThread.java

 


public class TimeoutThread{
    long milliseconds;
    private void notifyObj(){
        synchronized(this){
            notify();
        }
    }
    
    public TimeoutThread(long milliseconds){
        this.milliseconds = milliseconds;
    }
    
    public void mydo(){
        
    }
    
    Thread runthread = null;
    private void myrun(){
        
        class MyThread extends Thread{
            public void run(){
                try{
                    //监控程序执行时间不得超过
                    Thread.sleep(milliseconds);
                }catch(Exception exp){
                    //exp.printStackTrace();
                }
                //如果程序超出时间,停止原线程
                runthread.interrupt();
                
                //防止InterruptedException被捕获而杀不了,尝试20次
                int i=0;
                while(runthread.isAlive()){
                    try{
                    Thread.sleep(50);
                    }catch(Exception exp){}
                    runthread.interrupt();
                    i++;
                    if(i>20){
                        break;
                    }
                    System.out.println(i);
                }
                
                notifyObj();
            }
        }
        //将被监视线程指定为当前线程
        runthread = Thread.currentThread();
        //创建监控线程,设为非阻塞线程
        MyThread timeOutThread = new MyThread();
        timeOutThread.setDaemon(true);
        timeOutThread.start();
        mydo();
        timeOutThread.interrupt();
    }
    
    public void run(){
        try{
            new Thread(){
                public void run(){
                    try{
                        myrun();
                        notifyObj();
                    }catch(Exception exp){
                        notifyObj();
                        exp.printStackTrace();
                    }
                }
            }.start();
            synchronized(this){
                wait();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

 

TT.java

 


public class TT {

    public static String getName(long ms){
        final StringBuffer sb = new StringBuffer();
        new TimeoutThread(ms){
            public void mydo(){    
                try{
                    Thread.sleep(2000);
                    sb.append("hello world");
                }catch(Exception exp){
                    
                }
            }
        }.run();
        return sb.toString();
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("begin 5000 method");
        new TimeoutThread(5000){
            public void mydo(){    
                //该函数不能将InterruptedException捕获而不抛出,否则,无法起到监控效果
                //java.lang.InterruptedException
                //此函数显示了不能正确被关闭
                for(int i=0;i<20;i++){
                    try{
                        Thread.sleep(1000);
                    }catch(Exception exp){
                        //exp.printStackTrace();
                    }
                    System.out.println("step:"+i);
                }
                System.out.println("eeeee:");
            }
        }.run();
        
        System.out.println("end method");
        
        System.out.println("begin 5000 method");
        new TimeoutThread(5000){
            public void mydo(){    
                //该函数不能将InterruptedException捕获,否则,无法起到监控效果
                //java.lang.InterruptedException
                try{
                    for(int i=0;i<10;i++){
                        Thread.sleep(1000);
                        System.out.println("step:"+i);
                    }
                }catch(Exception exp){
                    //exp.printStackTrace();
                }
            }
        }.run();
        
        System.out.println("end method");
        
        System.out.println("get name is:"+getName(1000));
        System.out.println("get name is:"+getName(5000));
        
    }

}
posted @ 2016-03-23 17:56  donaldlee  阅读(2224)  评论(0编辑  收藏  举报