黑马程序员技术交流社区

标题: 线程中断的问题 [打印本页]

作者: 焦健    时间: 2012-12-24 20:16
标题: 线程中断的问题
本帖最后由 焦健 于 2012-12-25 12:11 编辑

使用interrupt方法中断线程,为什么中断不了啊?
在使用标记结束一个线程的时候,线程已经结束,为什么用isInterrupted方法判断却是false?
Thread类中的interrupt方法是用于中断线程,isInterrupted方法用于判断线程是否停止,如果该线程已经中断,则返回 true;否则返回 false。
我用来结果完全不一样,难道我用的不对吗,求高手指点,不多说,上代码:

class StopThread implements Runnable{
        private boolean b=true;
        public synchronized void run(){
                while(b){
                        //try{wait();}catch(Exception e){}
                        System.out.println(Thread.currentThread().getName()+"run");
                }
        }
        public void setb(boolean b){
                this.b=b;
        }
}
public class Duoshouhu {
        public static void main(String[] args) throws InterruptedException {
                StopThread st=new StopThread();
                Thread t1=new Thread(st);
                Thread t2=new Thread(st);
                //t1.setDaemon(true);//守护线程
                t1.start();
                t2.start();
                Thread.sleep(100);
                for(int num=0;true;num++){
                        if(num==60){//判断停止条件
                                st.setb(false);//使用标记结束线程
                                //t1.interrupt();//使用interrupt()方法使线程停止。
                                //t2.interrupt();
                                break;
                        }
                        System.out.println("main"+num);
                }
                Thread.sleep(100);
                //isInterrupted方法用于判断线程是否停止,如果该线程已经中断,则返回 true;否则返回 false。
                System.out.println(t1.isInterrupted());
                System.out.println(t2.isInterrupted());
        }

}

作者: 一帆风顺    时间: 2012-12-24 21:19
interrupt()不是停止线程的吧,是强制让冻结的线程恢复到运行中的吧


class StopThread implements Runnable{
        private boolean b=true;
        public synchronized void run(){
                while(b){
                        //try{wait();}catch(Exception e){}
                        System.out.println(Thread.currentThread().getName()+"run");
                }
        }
        public void setb(boolean b){
                this.b=b;
        }
}
public class Duoshouhu {
        public static void main(String[] args) throws InterruptedException {
                StopThread st=new StopThread();
                Thread t1=new Thread(st);
                Thread t2=new Thread(st);
        
                t1.start();
                t2.start();
                t1.sleep(100);             //由于这里让某一个线程睡了
                          
       for(int num=0;true;num++){
                        if(num==60){//判断停止条件
                                
                                   t1.interrupt();        //所以这里唤醒一个线程
                          t2.interrupt();
                                                        
                                 st.setb(false);//使用标记结束线程
                                  break;
                        }
                        System.out.println("main"+num);
                }
                           
                t2.sleep(100);
                //isInterrupted方法用于判断线程是否停止,如果该线程已经中断,则返回 true;否则返回 false
                System.out.println(t1.isInterrupted());       //所以这个输出的是true
                System.out.println(t2.isInterrupted());          //这里输出的是false
        }
}

个人见解,不对的请指出


作者: 焦健    时间: 2012-12-24 21:58
王博 发表于 2012-12-24 21:19
interrupt()不是停止线程的吧,是强制让冻结的线程恢复到运行中的吧

哦,原来是我理解错了,谢啦啊哥们




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2