当前位置: 代码迷 >> J2EE >> java 判断某个方法执行是否超时,该怎么解决
  详细解决方案

java 判断某个方法执行是否超时,该怎么解决

热度:176   发布时间:2016-04-22 02:32:49.0
java 判断某个方法执行是否超时
想在java程序中判断某个方法执行是否超时,比如说,如果该方法执行时间超过两秒,那么将结束本次程序执行
public class TimeoutException extends RuntimeException {
/**
* 序列化号
*/
private static final long serialVersionUID = -8078853655388692688L;

public TimeoutException(String errMessage)
{
super(errMessage);
System.out.println("2");
}


}

public class TimeoutThread extends Thread{
/**
* 计时器超时时间
*/
private long timeout;
  
/**
* 计时是否被取消
*/
private boolean isCanceled = false;
  
/**
* 当计时器超时时抛出的异常
*/
private TimeoutException timeoutException;
  
/**
* 构造器
* @param timeout 指定超时的时间
*/
public TimeoutThread(long timeout,TimeoutException timeoutErr) {
super();
this.timeout = timeout;
this.timeoutException = timeoutErr;
//设置本线程为守护线程
this.setDaemon(true);
}
  
/**
* 取消计时
*/
public synchronized void cancel()
{
isCanceled = true;
}

/**
* 启动超时计时器
*/
public void run()  
{
try {
System.out.println("run()");
Thread.sleep(timeout);
System.out.println("sleep");
if(!isCanceled) throw timeoutException;
} catch (InterruptedException e) {
System.out.println("eeeeeeeeee");
e.printStackTrace();
throw timeoutException;
}  
}

}

public class MainCheck {

/**
* @param args
*/
public static void main(String[] args) {

//初始化超时类
System.out.println("main");
TimeoutThread t = new TimeoutThread(1000,new TimeoutException("超时"));
System.out.println("t");
try{
System.out.println("t.start()-begin");
t.start();
System.out.println("t.start()-end");
//.....要检测超时的程序段....
getMothod();
t.cancel();
System.out.println("t.fail="+t.isfail);
System.out.println("t.cancel()-end");
}catch (Exception e)
{
按道理讲应该这里可以捕获到这个异常,可是怎么都进不来
System.out.println("eeeeeeeeeeeee");
//...对超时的处理...

}
System.out.println("main==end");
}
public static void getMothod(){
while(true){

}
}

}

------解决方案--------------------
Java code
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import com.sinosoft.lis.db.BOPBATINFDB;import com.sinosoft.lis.schema.BOPBATINFSchema;import com.sinosoft.utility.DBConn;import com.sinosoft.utility.DBConnPool;public class Test {        /**     * @param args     * @throws InterruptedException      * @throws SQLException      */    public static void main(String[] args) throws InterruptedException, SQLException {                Service service = new Service();        //开启一个线程去执行业务逻辑方法        System.out.println("测试开始");        service.start();                //为了简便我写个简单计时3秒        Thread.sleep(3000);        //2秒过后杀死执行业务逻辑方法的线程,停止业务逻辑方法的调用        //建议调用interrupt()中断程序而不建议调用stop();        service.interrupt();        //service.stop();                System.out.println("测试完毕");    }    }class Service extends Thread {    public void run() {        //执行业务逻辑方法        service();    }        public void service(){        try {            int i=0;            while(true){                                i++;                Thread.sleep(1000);                System.out.println(i);            }        } catch (InterruptedException e) {                        //e.printStackTrace();            System.out.println("程序被中断");        }    }        }
  相关解决方案