怎么让一个线程等待多次?

zhangj0571 2007-07-24 08:25:48
public static void main(String[] args) throws InterunpedException{
Thread t=Thread.currentThread();
System.out.println("1");
t.sleep(1000);

System.out.println("2");
t.sleep(1000);

System.out.println("3");
}

一运行就会跳出对话框,说 javaw.exe遇到问题需要关闭,不知道哪里有问题,大家帮帮忙啊
...全文
244 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangj0571 2007-07-24
  • 打赏
  • 举报
回复
还是看下面这个吧,程序只能打印1,2,3,4,没打印5,要怎么改下呢,郁闷
public class TestAmzi {
public void test() throws InterruptedException,IOException{


Thread t=Thread.currentThread();
System.out.println(1);

Runtime.getRuntime().exec("acmp test");

System.out.println(2);
t.sleep(1000);

System.out.println(3);
Runtime.getRuntime().exec("alnk test test");

System.out.println(4);
t.sleep(1000);

System.out.println(5);
LogicServer ls=null;

try {
ls=new LogicServer();
ls.Init("");
ls.Load("test");
long res=ls.CallStr("ura(user(lisi),role(g))");
System.out.println(res);

} catch(LSException e){
e.printStackTrace();
}finally{
if(ls!=null){
try{
ls.Close();
}catch(LSException e){
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws InterruptedException,IOException{
TestAmzi ta=new TestAmzi();
ta.test();



}
}
zhangj0571 2007-07-24
  • 打赏
  • 举报
回复
我只是想让pc和pl两个thread各自运行完(就是exec调用的命令行程序完成),再回到主程序里面继续向下执行
zhangj0571 2007-07-24
  • 打赏
  • 举报
回复
不好意思 刚才试了下,上面那个程序是可以运行的,我把我碰到问题的代码贴出来吧,大家帮忙分析下,分我会加的。
public class TestAmzi {
public void test() throws InterruptedException{


Thread t=Thread.currentThread();

Thread pc=new Thread(new PrologCompiler());
pc.start();

System.out.println(1);


t.sleep(1000);



System.out.println(2);
Thread pl=new Thread(new PrologLinker());
pl.start();

t.sleep(1000);

LogicServer ls=null;

try {

ls=new LogicServer();
ls.Init("");
ls.Load("test");
long res=ls.CallStr("ura(user(lisi),role(g))");
System.out.println(res);

} catch(LSException e){
e.printStackTrace();
}finally{
if(ls!=null){
try{
ls.Close();
}catch(LSException e){
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws InterruptedException{
TestAmzi ta=new TestAmzi();
ta.test();



}
}

PrologCompiler和PrologCompiler都是继承自Thread,只是运行了一个命令行程序而已。
public class PrologCompiler extends Thread {

public void run(){
try {
Runtime.getRuntime().exec("acmp test");
} catch (IOException e) {
e.printStackTrace();
}


}
}

上面程序打印1和2后就会跳出对话框,说 javaw.exe遇到问题需要关闭,不知道哪里有问题,大家帮帮忙啊
yjzagan2003 2007-07-24
  • 打赏
  • 举报
回复
我这样改了一下,不知是否你要的结果:
import java.io.InterruptedIOException;

public class TestThread {

public static void main(String[] args) throws InterruptedIOException, Exception{
Thread t=Thread.currentThread();
while(true){
System.out.println("1");
t.sleep(1000);

System.out.println("2");
t.sleep(1000);

System.out.println("3");
}
}
}
zhangj0571 2007-07-24
  • 打赏
  • 举报
回复
程序只会打印1和2,我觉得应该在第二个sleep那里有问题,有谁遇到过么?

62,614

社区成员

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

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