t.sleep(1000);
这个实际是主线程调用t的方法,本来就不科学,(在a线程里调用了b的sleep方法,实际上还是a去睡觉)
t = new Thread(this);
不清楚这个类是Thread子类还是实现了Runnable接口.
可以看下这个例子public class TestThread implements Runnable{
private static int delay = 1000;
private static int mainDelay = 500;
private static int flag = 1;
@Override
public void run() {
for(int i = 0; i
System.out.println("..."+i);
if(i%10==0||flag==0){
System.out.println("try sleep...i="+i);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
}
}
public static void main(String[] args) throws Exception {
Thread t = new Thread(new TestThread());
t.start();
for(int j = 0 ;j
Thread.sleep(mainDelay);
if(flag==1) flag = 0;
else flag = 1;
}
}
}