A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 韩俊杰 中级黑马   /  2012-12-20 23:37  /  1596 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 韩俊杰 于 2012-12-25 18:05 编辑

package practice;

class stopThreadDemo implements Runnable{
        private boolean flag=true;
        public synchronized void run(){
                while(flag){
                        try{
                                wait();
                        }catch(InterruptedException e){
                                System.out.println(Thread.currentThread().getName()+"...exception");
                                flag=false;
                        }
                        System.out.println(Thread.currentThread().getName()+"...run");
                }
        }
        
        public void ChangeFlag(){
                flag=false;
        }
}

public class StopDemo {
        public static void main(String[] args) {
                stopThreadDemo st=new stopThreadDemo();
               
                Thread th1=new Thread(st);
                Thread th2=new Thread(st);
                th1.setDaemon(true);
                th2.setDaemon(true);
                th1.start();
                th2.start();
               
                int num=0;
                while(true){
                        if (num++==60){
                                //st.ChangeFlag();
                               /*th1.interrupt();//强制清除冻结状态恢复到运行状态
                                th2.interrupt();*/
                                break;
                        }
                        System.out.println(Thread.currentThread().getName()+"..."+num);
                }

        }
}

有一点不明白就是什么是个守护线程?还有就是红颜色的两部分都能使线程结束,那应该用哪一个?

评分

参与人数 1技术分 +1 收起 理由
邵天强 + 1

查看全部评分

1 个回复

倒序浏览
请看我调试的结果吧,相信你会明白的
import java.io.*;


public class TestMain extends Thread {
   
    public TestMain() {
    }
    public void run() {
        for(int i = 1; i <= 10; i++){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println(i);
        }
                System.out.println("我结束了");
    }
   
    public static void main(String [] args)throws Exception{
        TestMain test = new TestMain();
        test.setDaemon(true);
        test.start();
        System.out.println("isDaemon = " + test.isDaemon());
                int i = 20;
       while(true)
           {
                        Thread.sleep(1000*3);
                        System.out.println(i--);
                        if(i==17)
                    {
                                System.out.println("主线程结束");
                                break;
                        }
                               
           }
    }
}


1、当普通线程运行结束时,本例的“主线程”,系统不管你这个守护线程有没有结束,我都退出
2、至于有什么实际应用,我也是菜鸟,不过我同意网上说的,jvm的垃圾回收线程,是一个守护线程。
3、还有输入法的后台线程,就是守护线程,它可以限制你的输入对不对,给出提示,我也同意这个说法。
4、有什么不对的,请各个师兄弟姐妹指出

守护.PNG (11.1 KB, 下载次数: 20)

守护线程例子运行结果

守护线程例子运行结果

评分

参与人数 1技术分 +1 收起 理由
邵天强 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马