如何在Java中使用Thread类的isAlive()方法?
如果线程处于活动状态,则Thread类的isAlive()方法返回true,这是在线程启动之后但运行完成之前的任何时间。
示例
class first implements Runnable {
public void run() {
try {
for(int i=0; i<=20; i+=2) {
Thread.sleep(600); System.out.println(i);
}
} catch(Exception e) {
}
}
class MyThread {
public static void main(String args[]) {
first obj = new first();
Thread thread = new Thread(obj);
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
System.out.println(thread.isAlive());
}
}输出结果
true 0 2 4 6 8 10 12 14 16 18 20
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短