java监控函数执行时间

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

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


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值