| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 441 人关注过本帖
标题:Daemon线程
取消只看楼主 加入收藏
yu1543054075
Rank: 1
等 级:新手上路
帖 子:102
专家分:8
注 册:2015-4-30
结帖率:90.24%
收藏
已结贴  问题点数:20 回复次数:1 
Daemon线程
public class ThreadA {
    public static void main(String[] args) {
        System.out.println("ThreadA执行:");
        
        Thread threadB = new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println("ThreadB开始:");
                    for (int i = 0; i < 5; i++ ) {
                        Thread.sleep(1000);
                        System.out.println("ThreadB执行..");
                    }
                    System.out.println("ThreadB即将结束..");
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        );
        threadB.start();
                threadB.setDaemon(true);//设置为Daemon线程
        System.out.println("ThreadA执行..");
    }
}
C:\Users\Administrator\Desktop\classes>java ThreadA
ThreadA执行:
ThreadB开始:
Exception in thread "main" java.lang.IllegalThreadStateException
        at java.lang.Thread.setDaemon(Thread.java:1275)
        at ThreadA.main(ThreadA.java:121)
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB即将结束..
为什么没有执行System.out.println("ThreadA执行..");这个语句,求指导
搜索更多相关主题的帖子: public 
2015-06-08 19:54
yu1543054075
Rank: 1
等 级:新手上路
帖 子:102
专家分:8
注 册:2015-4-30
收藏
得分:0 
非常感谢版主的解释,那么下面的是怎么回事?
public class ThreadA {
    public static void main(String[] args) {
        System.out.println("ThreadA执行:");
        
        Thread threadB = new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println("ThreadB开始:");
                    for (int i = 0; i < 5; i++ ) {
                        Thread.sleep(1000);
                        System.out.println("ThreadB执行..");
                    }
                    System.out.println("ThreadB即将结束..");
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        );
        threadB.setDaemon(true);//设置为Daemon线程
        threadB.start();
        System.out.println("ThreadA执行");
    }
}
//
//C:\Users\Administrator\Desktop\classes>java ThreadA
//ThreadA执行
//ThreadA执行
//ThreadB开始
为什么不是ThreadA执行
          ThreadB开始
          ThreadA执行
求指导!!
2015-06-10 17:42
快速回复:Daemon线程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016766 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved