当前位置: 技术问答>java相关
高分提问易懂正确就给分
来源: 互联网 发布时间:2015-03-28
本文导语: 今天看Thinking in JAVA时对Daemon线程有些看不明白,各位大侠能不能非讲一讲Daemon线程的作用、优点并举一个例子,或者有什么参考资料发给我也行,分数不是问题,但一定要正确,说的明白 | ...
今天看Thinking in JAVA时对Daemon线程有些看不明白,各位大侠能不能非讲一讲Daemon线程的作用、优点并举一个例子,或者有什么参考资料发给我也行,分数不是问题,但一定要正确,说的明白
|
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;
}
|
14.1.5 Daemon线程
“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
“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