Daemon线程即服务线程,是为其它线程服务的线程,如果程序中只有服务线程,那程序就会停止运行,因为程序中只有服务线程是毫无意义的!比如记时器就是很好的服务线程,如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class TimerTest
{ public static void main(String[] args)
{ JFrame f = new TimerTestFrame();
f.show();
}
}class TimerTestFrame extends JFrame
{ public TimerTestFrame()
{ setSize(450, 300);
setTitle("TimerTest"); addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} ); Container c = getContentPane();
c.setLayout(new GridLayout(2, 3));
c.add(new ClockCanvas("San Jose", "GMT-8"));
c.add(new ClockCanvas("Taipei", "GMT+8"));
c.add(new ClockCanvas("Berlin", "GMT+1"));
c.add(new ClockCanvas("New York", "GMT-5"));
c.add(new ClockCanvas("Cairo", "GMT+2"));
c.add(new ClockCanvas("Bombay", "GMT+5"));
}
}interface TimerListener
{ void timeElapsed(Timer t);
}class Timer extends Thread
{ public Timer(int i, TimerListener t)
{ target = t;
interval = i;
setDaemon(true);
} public void run()
{ try
{ while (!interrupted())
{ sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
} private TimerListener target;
private int interval;
}class ClockCanvas extends JPanel
implements TimerListener
{ public ClockCanvas(String c, String tz)
{ city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(125, 125);
} public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawOval(0, 0, 100, 100);
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;
g.drawLine(50, 50, 50 + (int)(30
* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(50, 50, 50 + (int)(40
* Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(50, 50, 50 + (int)(45
* Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));
g.drawString(city, 0, 115);
} public void timeElapsed(Timer t)
{ calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();
} private int seconds = 0;
private String city;
private int offset;
private GregorianCalendar calendar; private final int LOCAL = 16;
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class TimerTest
{ public static void main(String[] args)
{ JFrame f = new TimerTestFrame();
f.show();
}
}class TimerTestFrame extends JFrame
{ public TimerTestFrame()
{ setSize(450, 300);
setTitle("TimerTest"); addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} ); Container c = getContentPane();
c.setLayout(new GridLayout(2, 3));
c.add(new ClockCanvas("San Jose", "GMT-8"));
c.add(new ClockCanvas("Taipei", "GMT+8"));
c.add(new ClockCanvas("Berlin", "GMT+1"));
c.add(new ClockCanvas("New York", "GMT-5"));
c.add(new ClockCanvas("Cairo", "GMT+2"));
c.add(new ClockCanvas("Bombay", "GMT+5"));
}
}interface TimerListener
{ void timeElapsed(Timer t);
}class Timer extends Thread
{ public Timer(int i, TimerListener t)
{ target = t;
interval = i;
setDaemon(true);
} public void run()
{ try
{ while (!interrupted())
{ sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
} private TimerListener target;
private int interval;
}class ClockCanvas extends JPanel
implements TimerListener
{ public ClockCanvas(String c, String tz)
{ city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(125, 125);
} public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawOval(0, 0, 100, 100);
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;
g.drawLine(50, 50, 50 + (int)(30
* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(50, 50, 50 + (int)(40
* Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(50, 50, 50 + (int)(45
* Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));
g.drawString(city, 0, 115);
} public void timeElapsed(Timer t)
{ calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();
} private int seconds = 0;
private String city;
private int offset;
private GregorianCalendar calendar; private final int LOCAL = 16;
}
“Daemon”线程的作用是在程序的运行期间于后台提供一种“常规”服务,但它并不属于程序的一个基本部分。因此,一旦所有非Daemon线程完成,程序也会中止运行。相反,假若有任何非Daemon线程仍在运行(比如还有一个正在运行main()的线程),则程序的运行不会中止。
通过调用isDaemon(),可调查一个线程是不是一个Daemon,而且能用setDaemon()打开或者关闭一个线程的Daemon状态。如果是一个Daemon线程,那么它创建的任何线程也会自动具备Daemon属性。
下面这个例子演示了Daemon线程的用法://: Daemons.java
// Daemonic behavior
import java.io.*;class Daemon extends Thread {
private static final int SIZE = 10;
private Thread[] t = new Thread[SIZE];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i < SIZE; i++)
t[i] = new DaemonSpawn(i);
for(int i = 0; i < SIZE; i++)
System.out.println(
"t[" + i + "].isDaemon() = "
+ t[i].isDaemon());
while(true)
yield();
}
}class DaemonSpawn extends Thread {
public DaemonSpawn(int i) {
System.out.println(
"DaemonSpawn " + i + " started");
start();
}
public void run() {
while(true)
yield();
}
}public class Daemons {
public static void main(String[] args) {
Thread d = new Daemon();
System.out.println(
"d.isDaemon() = " + d.isDaemon());
// Allow the daemon threads to finish
// their startup processes:
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Waiting for CR");
try {
stdin.readLine();
} catch(IOException e) {}
}
} ///:~Daemon线程可将自己的Daemon标记设置成“真”,然后产生一系列其他线程,而且认为它们也具有Daemon属性。随后,它进入一个无限循环,在其中调用yield(),放弃对其他进程的控制。在这个程序早期的一个版本中,无限循环会使int计数器增值,但会使整个程序都好象陷入停顿状态。换用yield()后,却可使程序充满“活力”,不会使人产生停滞或反应迟钝的感觉。
一旦main()完成自己的工作,便没有什么能阻止程序中断运行,因为这里运行的只有Daemon线程。所以能看到启动所有Daemon线程后显示出来的结果,System.in也进行了相应的设置,使程序中断前能等待一个回车。如果不进行这样的设置,就只能看到创建Daemon线程的一部分结果(试试将readLine()代码换成不同长度的sleep()调用,看看会有什么表现)。