Luckylau's Blog

Java并发之守护线程

java的Thread类和Runnable接口

守护线程

​ Thread有两种线程,即“守护线程Daemon”与“用户线程User”。守护线程的优先级非常低,通常在程序里没有其他线程运行时才会执行它。当守护线程是程序里唯一在运行的线程时,JVM会结束守护线程并终止程序。守护线程通常用于在同一程序里给普通线程(也叫使用者线程)提供服务。它们通常无限循环的等待服务请求或执行线程任务。它们不能做重要的任务,因为我们不知道什么时候会被分配到CPU时间片,并且只要没有其他线程在运行,它们可能随时被终止。JAVA中最典型的这种类型代表就是垃圾回收器。

setDaemon(boolean on)方法可以方便的设置线程的Daemon模式,true为Daemon模式,此方法必须在线程启动之前调用,当线程正在运行时调用会产生异常。当一个守护线程中产生了其他线程,那么这些新产生的线程不用设置Daemon属性,都将是守护线程。Java垃圾回收线程就是一个典型的守护线程,当我们的程序中不在有任何运行中的Thread,程序就不会再产生垃圾,垃圾回收器也就无事可做,所以当垃圾回收线程是Java虚拟机上仅剩的线程时,Java虚拟机会自动离开。

示例一:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class DataSourcesLoader implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.printf("Beginning data sources loading : %s\n", new Date());
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("Data sources loading has finished :%s\n",new Date());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.concurrent.TimeUnit;
public class Daemon extends Thread {
private int times =1;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
TimeUnit.SECONDS.sleep(1)
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("后台运行的守护线程:"+(times ++) + "s");
}
}
}
1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
DataSourcesLoader dsLoader = new DataSourcesLoader();
Daemon daemon =new Daemon();
daemon.setDaemon(true);
daemon.start();
Thread thread1 = new Thread(dsLoader,"DataSourcesLoader");
thread1.start();
}
}

示例二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Deque;
import java.util.concurrent.TimeUnit;
public class CleanerTask extends Thread {
private Deque<Event> deque;
public CleanerTask(Deque<Event> deque) {
this.deque = deque;
this.setDaemon(true);
}
@Override
public void run() {
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
// TODO: handle exception
}
System.out.printf("Cleaner is running ....\n");
if (deque.size()==0) {
System.out.println("deque is empty");
}else{
Event e = deque.getLast();
System.out.printf("Cleaner: %s\n",e.getEvent());
deque.removeLast();
System.out.printf("Cleaner: Size of the queue: %d\n",deque. size());
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Date;
import java.util.Deque;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class WriterTask implements Runnable{
private Deque<Event> deque;
private Random rand =new Random();
public WriterTask (Deque<Event> deque){
this.deque=deque;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i=1; i<100; i++) {
Event event=new Event();
event.setDate(new Date());
event.setEvent(String.format("The thread %s has generated an event",Thread.currentThread().getId()));
deque.addFirst(event);
System.out.println("The thread "+Thread.currentThread().getId()+" has generated an event");
try {
TimeUnit.SECONDS.sleep(rand.nextInt(20));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Date;
public class Event {
private Date date;
private String event;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Event> deque = new ArrayDeque<Event>();
WriterTask writer = new WriterTask(deque);
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(writer);
thread.start();
}
CleanerTask cleaner = new CleanerTask(deque);
cleaner.start();
}
}
Luckylau wechat
如果对您有价值,看官可以打赏的!