我怎么样可以在一个线程,关掉另一个关掉一个阻塞中的线程(interrupt()不起作用)
public class JobThread extends TimerTask {
List map = new ArrayList(); public void run() {
for(int i=0;i<map.size();i++){
ManagerThread th = (ManagerThread) map.get(i);
th.flag = false;
th.interrupt();
System.out.println(th.getName() + "call Timer work");
}
}
}package thread;import java.util.Timer;
public class ManagerThread extends Thread{
boolean flag = true;
public void run() {
long now = System.currentTimeMillis();
while(flag){
for(long i=10000;i<10000000000L;i++){
// System.out.println("运行中....."+(i*i));
}
flag = false;
}
System.out.println(this.getName()+"停止.....");
System.out.println("11111111111 "+(System.currentTimeMillis()-now));
}
public static void main(String[] args) {
ManagerThread th1 = new ManagerThread();
// ManagerThread th2 = new ManagerThread();
JobThread th3 = new JobThread();
// th3.map.add(th1);
// th3.map.add(th2);
th1.start();
// th2.start();
Timer timer = new Timer();
timer.schedule(th3, 1000);
}
}
public class JobThread extends TimerTask {
List map = new ArrayList(); public void run() {
for(int i=0;i<map.size();i++){
ManagerThread th = (ManagerThread) map.get(i);
th.flag = false;
th.interrupt();
System.out.println(th.getName() + "call Timer work");
}
}
}package thread;import java.util.Timer;
public class ManagerThread extends Thread{
boolean flag = true;
public void run() {
long now = System.currentTimeMillis();
while(flag){
for(long i=10000;i<10000000000L;i++){
// System.out.println("运行中....."+(i*i));
}
flag = false;
}
System.out.println(this.getName()+"停止.....");
System.out.println("11111111111 "+(System.currentTimeMillis()-now));
}
public static void main(String[] args) {
ManagerThread th1 = new ManagerThread();
// ManagerThread th2 = new ManagerThread();
JobThread th3 = new JobThread();
// th3.map.add(th1);
// th3.map.add(th2);
th1.start();
// th2.start();
Timer timer = new Timer();
timer.schedule(th3, 1000);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货