ITeye博客

java, user thread,deamon thread

chbaowang · 2012-03-14 16:48 · core java · 转帖 · 编程语言 · Markdown

Java中User Thread和Daemon Thread的区别 http://blog.csdn.net/liangliyin/article/details/6076552

Java将线程分为User线程和Daemon线程两种。通常Daemon线程用来为User线程提供某些服务。程序的main()方法线程是一个User进程。User进程创建的进程为User进程。当所有的User线程结束后,JVM才会结束。

通过在一个线程对象上调用setDaemon(true),可以将user线程创建的线程明确地设置成Daemon线程。例如,时钟处理线程、idle线程、垃圾回收线程、屏幕更新线程等,都是Daemon线程。通常新创建的线程会从创建它的进程哪里继承daemon状态,除非明确地在线程对象上调用setDaemon方法来改变daemon状态。

需要注意的是,setDaemon()方法必须在调用线程的start()方法之前调用。一旦一个线程开始执行(如,调用了start()方法),它的daemon状态不能再修改。通过方法isDaemon()可以知道一个线程是否Daemon线程。

通过执行下面的代码,可以很清楚地说明daemon的作用。当设置线程t为Daemon线程时,只要User线程(main线程)一结束,程序立即退出,也就是说Daemon线程没有时间从10数到1。但是,如果将线程t设成非daemon,即User线程,则该线程可以完成自己的工作(从10数到1)。

view plaincopyprint?import static java.util.concurrent.TimeUnit.*;
public class DaemonTest {
public static void main(String args) throws InterruptedException {
Runnable r = new Runnable() {
public void run() {
for (int time = 10; time > 0; --time) {
System.out.println("Time #" + time);
try {
SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};

    Thread t = new Thread(r);  
    t.setDaemon(true);  // try to set this to "false" and see what happens   
    t.start();  
      
    System.out.println("Main thread waiting...");  
    SECONDS.sleep(6);  
    System.out.println("Main thread exited.");  
}  

}
import static java.util.concurrent.TimeUnit.*; public class DaemonTest { public static void main(String args) throws InterruptedException { Runnable r = new Runnable() { public void run() { for (int time = 10; time > 0; --time) { System.out.println("Time #" + time); try { SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } };

    Thread t = new Thread(r);
    t.setDaemon(true);  // try to set this to "false" and see what happens
    t.start();
    
    System.out.println("Main thread waiting...");
    SECONDS.sleep(6);
    System.out.println("Main thread exited.");
}

}

t为Daemon线程的输出: Time #10

Time #9

Time #8

Main thread exited.

Time #7

t为User线程的输出: Main thread waiting...

Time #10

Time #9

Time #8

Main thread exited.

Time #7

Time #6

Time #5

Time #4

Time #3

Time #2

Time #1


JAVA并发编程——守护线程(Daemon Thread) http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html

在Java中有两类线程:用户线程 (User Thread)、守护线程 (Daemon Thread)。

所谓守护 线程,是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称职的守护者,并且这种线程并不属于程序中不可或缺的部分。因此,当所有的非守护线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。反过来说,只要任何非守护线程还在运行,程序就不会终止。

用户线程和守护线程两者几乎没有区别,唯一的不同之处就在于虚拟机的离开:如果用户线程已经全部退出运行了,只剩下守护线程存在了,虚拟机也就退出了。 因为没有了被守护者,守护线程也就没有工作可做了,也就没有继续运行程序的必要了。

将线程转换为守护线程可以通过调用Thread对象的setDaemon(true)方法来实现。在使用守护线程时需要注意一下几点:

(1) thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。

(2) 在Daemon线程中产生的新线程也是Daemon的。

(3) 守护线程应该永远不去访问固有资源,如文件、数据库,因为它会在任何时候甚至在一个操作的中间发生中断。

代码示例:

import java.util.concurrent.TimeUnit;

/**

  • 守护线程 */ public class Daemons {

    /**

    • @param args

    • @throws InterruptedException */ public static void main(String args) throws InterruptedException {

      Thread d = new Thread(new Daemon());

      d.setDaemon(true); //必须在启动线程前调用

      d.start();

      System.out.println("d.isDaemon() = " + d.isDaemon() + ".");

      TimeUnit.SECONDS.sleep(1); } }

class DaemonSpawn implements Runnable {

public void run() {
    while (true) {
        Thread.yield();
    }
}

}

class Daemon implements Runnable {

private Thread t = new Thread;

public void run() {

    for (int i=0; i<t.length; i++) {

        t* = new Thread(new DaemonSpawn());

        t*.start();

        System.out.println("DaemonSpawn " + i + " started.");
    }

    for (int i=0; i<t.length; i++) {

        System.out.println("t.isDaemon() = " +

                t*.isDaemon() + ".");
    }

    while (true) {
        Thread.yield();
    }
}

}

运行结果:

d.isDaemon() = true.

DaemonSpawn 0 started.

DaemonSpawn 1 started.

DaemonSpawn 2 started.

DaemonSpawn 3 started.

DaemonSpawn 4 started.

DaemonSpawn 5 started.

DaemonSpawn 6 started.

DaemonSpawn 7 started.

DaemonSpawn 8 started.

DaemonSpawn 9 started.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

t.isDaemon() = true.

以上结果说明了守护线程中产生的新线程也是守护线程。

如果将mian函数中的TimeUnit.SECONDS.sleep(1);注释掉,运行结果如下:

d.isDaemon() = true.

DaemonSpawn 0 started.

DaemonSpawn 1 started.

DaemonSpawn 2 started.

DaemonSpawn 3 started.

DaemonSpawn 4 started.

DaemonSpawn 5 started.

DaemonSpawn 6 started.

DaemonSpawn 7 started.

DaemonSpawn 8 started.

DaemonSpawn 9 started.

以上结果说明了如果用户线程已经全部退出运行了,只剩下守护线程存在了,虚拟机也就退出了。下面的例子也说明了这个问题。

代码示例:

import java.util.concurrent.TimeUnit;

/**

  • Finally shoud be always run ? */

public class DaemonsDontRunFinally {

/**
 * @param args
 */
public static void main(String args) {

    Thread t = new Thread(new ADaemon());

    t.setDaemon(true);

    t.start();
}

}

class ADaemon implements Runnable {

public void run() {

    try {
        System.out.println("start ADaemon...");

        TimeUnit.SECONDS.sleep(1);

    } catch (InterruptedException e) {
        System.out.println("Exiting via InterruptedException");
    } finally {

        System.out.println("This shoud be always run ?");

    }
}

}

运行结果:

start ADaemon...

如果将main函数中的t.setDaemon(true);注释掉,运行结果如下:

start ADaemon...

This shoud be always run ?

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。