在主线程中启动了一个子线程先已知子线程被阻塞,是因为url连接建立,阻塞在读取数据方面,因此通过标识符判断使子线程自动结束的方法无法使用了先就请教,如何在主线程中直接kill掉这个子线程了使用了
t.stop()//t为子线程引用
或者
try {
t.sleep(300);
} catch (InterruptedException x) {
t.interrupt();//中断线程
}
2个方法均无法直接kill掉线程t,因为在执行方法之前之后,执行t.isAlive()均为true不知道现在有没有其他别的方法能够使用了
t.stop()//t为子线程引用
或者
try {
t.sleep(300);
} catch (InterruptedException x) {
t.interrupt();//中断线程
}
2个方法均无法直接kill掉线程t,因为在执行方法之前之后,执行t.isAlive()均为true不知道现在有没有其他别的方法能够使用了
public void run() {
boolean exit = false;
while (!exit) {
try {
sleep(300);
} catch (InterruptedException x) {
exit = true;
}
}
System.out.println("exit....");
}
};
th.start(); try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
th.interrupt();
要在主进程中,控制线程的存活,控制run()方法便可,常用的方式是:
public void run() {
while(flag) {
....
}
}控制flag(boolean)的值,便可以控制这个线程的存活。
记得Thread类有一个静态方法,判断该线程是否正占用当前cup资源。