`
frank-liu
  • 浏览: 1666996 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java concurrency: daemon线程

 
阅读更多

daemon线程的概念

        在学习操作系统概念的时候,我们就曾听说过daemon的概念。daemon本身指的是在后台运行的进程或者线程,一般用来提供某些不需要与用户直接交互的服务,有点像我们见到的一些系统服务。在java线程中,一般可以分为两类,一类是普通的线程,就是那些我们通过常用的Thread类或者Runnable接口实现并启动的类。还有一类是daemon线程。这种线程也通过和创建普通线程同样的方式来创建,不过需要通过setDaemon方法设置为daemon线程。

        daemon线程有几个典型的特征:

        1. daemon线程是运行于后台的,不和用户直接交互。

        2. 相对普通的线程来说,daeomn线程可以说是普通线程的下属,它的优先级要低一些。

        3. daemon线程具有一定程度的继承性,这个继承性不是指类的继承,而是指当一个daemon线程创建一个新线程的话,这个新创建的线程就默认为daemon线程了。

        4. 和普通线程的退出机制不同,在jvm即将关闭的时候,普通线程需要执行一系列的记录或者退出方法,比如需要执行finalize方法的则需要执行这部分,有finally块的代码也必须执行。而daemon线程退出的时候,jvm直接将它丢弃并退出,里面就算有finally块定义的代码也不会被执行。

 

一个daemon线程的示例

        创建或者使用daemon线程一般包括一下3个步骤:

1. 如果线程本身就是daemon线程了,那么通过常规线程创建手段创建出来的就已经是daemon线程。

2. 如果通过普通线程创建创建出来的话,需要在启动线程前调用setDaemon()方法。切记,一定要在调用start()方法前。

3.我们可以通过调用isDaemon()方法来判断一个线程是否为daeomn.

下面是示例代码:

class MyDaemon implements Runnable
{
    Thread thrd;

    MyDaemon()
    {
        // Create the thread
        thrd = new Thread(this);

        // Set to daemon
        thrd.setDaemon(true);

        // Start the thread
        thrd.start();
    }

    public void run()
    {
        try
        {
            for(;;)
            {
                System.out.print(".");
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException exc)
        {
            System.out.println("MyDaemon interrupted.");
        }
    }
}

class DaemonDemo
{
    public static void main(String[] args)
    {
        MyDaemon dt = new MyDaemon();
        if(dt.thrd.isDaemon())
            System.out.println("dt is a daemon thread.");

        System.out.println("Sleeping in main thread.");

        try
        {
            Thread.sleep(10000);
        }
        catch(InterruptedException exc)
        {
            System.out.println("Main thread interrupted.");
        }

        System.out.println("\nMain thread ending.");
    }
}

 在上面的代码中,我们实现Runnable接口定义一个MyDaemon类,在构造函数中调用setDaemon()设置该线程为daemon.在main方法中启动MyDaemon线程之后,main方法结束时daemon线程也自动结束了。上述代码的执行结果如下:

dt is a daemon thread.
Sleeping in main thread.
..........
Main thread ending.
 

一个比较有意思的事情就是,如果我们将前面代码里setDaemon()方法的这一行注释了,也就是说将该线程设置为普通的用户线程,再执行上面的代码时,我们会发现,程序不会终止,会一直执行下去,虽然主线程已经结束了。其输出的结果会像如下所示:

Sleeping in main thread.
..........
Main thread ending.
.......................

设置daemon线程的意义

        从前面的理解来看,daemon线程有点像一个我们画的草图,随时可以丢弃。它一般是用于在jvm关闭的时候,我们需要启动一些线程做一些辅助性的工作,但是我们又不希望这种工作妨碍到jvm正常关闭。和其他正常的线程来说,或许我们应该称之为不碍事的线程更合适。

daemon线程的一些应用

        daemon线程在java中有很多地方用到,一个典型的就是垃圾回收的GC线程。另外如果我们需要用线程做一些比如时间提醒,标记等事情,采用daemon线程也是一个比较合适的选择。

 

又一个daemon的示例

        根据前面的理解,我们再尝试一个更复杂的daemon应用。假定我们在一个应用中需要定义一些时间通知,比如说设置在某个指定的时候弹出一个会议通知的提醒。这是一个比较常见的场景,可以用daemon线程来实现。

具体的代码实现如下:

import java.util.*;

class Reminder implements Runnable
{
    Calendar reminderTime;

    String message;

    Reminder(String msg, int delay)
    {
        message = msg;
        
        // Get the current time and date.
        reminderTime = Calendar.getInstance();

        // Add the delay to the time and date.
        reminderTime.add(Calendar.SECOND, delay);

        System.out.printf("Reminder set for %tD %1$tr\n", reminderTime);


        // Create the reminder thread.
        Thread dThrd = new Thread(this);

        // Set to daemon
        dThrd.setDaemon(true);

        // Start execution.
        dThrd.start();
    }

    // Run the reminder.
    public void run()
    {
        try
        {
            for(;;)
            {
                // Get the current time and date.
                Calendar curTime = Calendar.getInstance();

                // See if it's time for the reminder.
                if(curTime.compareTo(reminderTime) >= 0)
                {
                    System.out.println("\n" + message + "\n");
                    break;
                }

                Thread.sleep(1000);
            }
        }
        catch(InterruptedException exc)
        {
            System.out.println("Reminder interrupted.");
        }
    }
}

class ReminderDemo
{
    public static void main(String[] args)
    {
        // Get a reminder 2 seconds from now.
        Reminder mt = new Reminder("Call Harry", 2);

        // Keep the main thread alive for 20 seconds.
        for(int i = 0; i < 20; i++)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException exc)
            {
                System.out.println("Main thread interrupted.");
            }
            System.out.print(".");
        }

        System.out.println("\nMain thread ending.");
    }
}

         我们设置了一个daemon线程,在一个无限循环里比较当前时间和指定的时间,在达到指定的时间时则显示一个提醒消息。上述代码的输出如下:

Reminder set for 10/21/12 01:57:10 PM
..
Call Harry

..................
Main thread ending.

总结

        悄悄的,daemon线程走了,正如daemon线程悄悄的来;挥一挥手,带不走jvm的一片云彩:)

 

参考资料

Herb Schildt’s Java Programming Cookbook

 

java concurrency in practice

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics