Java里有一种特殊的线程叫做守护(Daemon)线程,这种线程的优先级很低,通常来说,当一个应用程序里面没有其他线程运行的时候,守护线程才运行,当线程是程序中唯一运行的线程时,守护线程执行结束后,JVM也就结束了这个程序。因此,守护线程通常被用来作为同一程序中普通线程的服务提供者,通常是无线循环的,以等待服务请求或者线程任务。
代码实现
1:创建Event类,声明两个私有属性
package com.packtpub.java7.concurrency.chapter1.recipe7.event; import java.util.Date; /** * Class that stores event's information * */ public class Event { /** * Date of the event */ private Date date; /** * Message of the event */ private String event; /** * Reads the Date of the event * @return the Date of the event */ public Date getDate() { return date; } /** * Writes the Date of the event * @param date the date of the event */ public void setDate(Date date) { this.date = date; } /** * Reads the message of the event * @return the message of the event */ public String getEvent() { return event; } /** * Writes the message of the event * @param event the message of the event */ public void setEvent(String event) { this.event = event; } }