某段程序执行一段时间以后没有完成的话自动跳过的功能?

jiejue1984 2008-04-30 03:31:47
public static void main(String[] args) {
String aaa= "原来的数据";

aaa=getAAA();

System.out.println("aaa="+aaa);
}

private static String getAAA(){

/*我在这边写了一个死循环*/
return "getaaa";
}

我想监控aaa=getAAA()这段代码.如果这段代码执行时间超过10秒就自动跳过.输出结果为
aaa=原来的数据

我不知道有没有描述清楚我的问题.

请大侠们给点思路.谢谢
...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiejue1984 2008-04-30
  • 打赏
  • 举报
回复
非常感谢已经完成.学到了东西.谢谢jiangnaisong
云上飞翔 2008-04-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jiejue1984 的回复:]

重发一下代码

Java code
/**
* @author XuHuiJun
*
*/
public class Test {

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

String aaa = "原来的数据";

try {

MyThread aa = new MyThread();
aa.start();
//设置最长等待两秒
aa.join(2 * 1000);
aaa = aa.get…
[/Quote]
以下代码供你参考:(已测试通过)

public class TestThread
{
public static void main(String[] args) {

String aaa = "原来的数据";

try {

MyThread aa = new MyThread();
aa.start();
//设置最长等待两秒
aa.join(2 * 1000);
aaa = aa.getAAA()!=null?aa.getAAA():aaa;
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("aaa=" + aaa);
}

}

class MyThread extends Thread {
private String rs=null;
public String getAAA()
{
return rs;
}
private String _getAAA() {
//你的真正代码放在此处
long start = System.currentTimeMillis();
long end = 0;
while (true) {
end = System.currentTimeMillis();
// System.out.println((end - start)/1000);
if ((end - start) / 1000 >= 10) {// 已经过10秒
return "getaaa";
}
}
}
public void run()
{
rs=_getAAA();
}

}


云上飞翔 2008-04-30
  • 打赏
  • 举报
回复
答:你的getAAA()线程代码写得不对啊。
jiejue1984 2008-04-30
  • 打赏
  • 举报
回复

重发一下代码

/**
* @author XuHuiJun
*
*/
public class Test {

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

String aaa = "原来的数据";

try {

MyThread aa = new MyThread();
aa.start();
//设置最长等待两秒
aa.join(2 * 1000);
aaa = aa.getAAA();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("aaa=" + aaa);
}

}

class MyThread extends Thread {

public String getAAA() {

long start = System.currentTimeMillis();
long end = 0;
while (true) {
end = System.currentTimeMillis();
System.out.println((end - start)/1000);
if ((end - start) / 1000 >= 10) {// 已经过10秒
return "getaaa";
}
}
}

}


jiejue1984 2008-04-30
  • 打赏
  • 举报
回复
根据jiangnaisong 的建议改成下面的代码.不过执行结果还是aaa=getaaa,没有在2秒后停止执行.不知道我理解错了还是什么?请继续指教

public class Test {

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

String aaa = "原来的数据";

try {

MyThread aa = new MyThread();
aa.start();
//设置最长等待两秒
aa.join(2 * 1000);
aaa = aa.getAAA();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("aaa=" + aaa);
}

}

class MyThread extends Thread {

public String getAAA() {

long start = System.currentTimeMillis();
long end = 0;
while (true) {
end = System.currentTimeMillis();
if ((end - start) / 1000 >= 10) {// 已经过10秒
return "getaaa";
}
}
}

}
云上飞翔 2008-04-30
  • 打赏
  • 举报
回复
答:用join(10*1000)的含义表示main最多等待getAAA()10秒,时间一到,main()不管getAAA()都自己继续运行。
云上飞翔 2008-04-30
  • 打赏
  • 举报
回复
答:在ThreadB.join(10*1000);之前先加上:ThreadB.start();
anqini 2008-04-30
  • 打赏
  • 举报
回复

package zhao;

public class Test11{
public static void main(String[] args) {
String aaa= "原来的数据";

aaa=getAAA();

System.out.println("aaa="+aaa);
}

private static String getAAA(){

long start = System.currentTimeMillis();
long end = 0;
while(true) {
System.out.print("hi");
end = System.currentTimeMillis();
if((end-start)/1000>=10) {//已经过10秒
return "getaaa";
}
}
//return "getaaa";
}
}
a_nuo 2008-04-30
  • 打赏
  • 举报
回复
把你要运行的方法让一格TimerTask类中的run方法调用
然后就用Timer调用TimerTask并设定执行时间,就可以了
云上飞翔 2008-04-30
  • 打赏
  • 举报
回复
答:将private static String getAAA()包装成一个线程。设为ThreadB。
则:String aaa= "原来的数据";

ThreadB.join(10*1000);//最多等待10秒。否则自己继续运行
System.out.println("aaa="+aaa);

a_nuo 2008-04-30
  • 打赏
  • 举报
回复
你用Timer类和TimerTask就可以了
丰2007 2008-04-30
  • 打赏
  • 举报
回复
需要使用线程来控制吧

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧