in Java there is a special thread called the Daemon Thread (Daemon) thread. This thread has a low priority, and typically, when no other thread in the same application is running, the guard line friend run. When the only thread running in the program is the daemon thread, and the daemon finishes executing, the JVM ends the program.
Because of this feature, the daemon thread is often used as a service provider for ordinary threads (user threads) in the same program. They are typically wireless-looped to wait for a service request or to perform a thread's task. They cannot do important work because it is impossible to know when the daemon gets the CPU clock, and when no other thread is running, the daemon thread can end at any time. A typical application is Java GC.
How to create a daemon thread and write a simple demo. The scenario is as follows: There are two threads, one is the user thread, and it writes the events to a queue. The other is the daemon thread. He will manage this queue. If the generated event exceeds 10 seconds, it will be removed.
1. Create an event class. Declare two private properties in a class, one Date Type property date, and another string Type property event. and generate read and write methods for both properties.
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;
}
2. Create the Writertask class to implement the Runnable interface.
public class Writertask implements runnable{}
3. Declare a queue that holds the event object and implement the constructor with parameters to initialize the queue object.
Deque<event> Deque;
Public Writertask (deque<event> Deque) {
This.deque = deque;
}
4. Implement the thread's run () method. It will perform 100 cycles. In each loop, a new event object is created and placed in the queue. Then hibernate for a second.
Public void Run () {
for (int i=0;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);
try{
TimeUnit.SECONDS.sleep (1);
}catch (interruptedexception e) {
e.printstacktrace ();
}
}
}
5. Create the Cleanertask class and inherit the Thrad class.
public class Cleanertask extends thread{}
6. Declare the queue that holds the event object and implement the constructor to initialize the queue object. At the same time, in this constructor, the thread is set to the daemon thread through the Setdaemon () method.
Deque<event> Deque;
Public Cleanertask (deque<event> Deque) {
This.deque = deque;
Setdaemon (TRUE);
}
7. Implement the Run () method, which will run indefinitely and will fetch the current time in each run and call the Clean () method.
public void Run () {
while (true) {
Date Date=new date ();
Clean (date);
}
}
8. Implement the Clean () method. The clean () method reads the last event object of the queue, and if the event was created before 10 seconds, it is deleted and checked for the next one. If an event is deleted, clean () tells the print queue length that the event information is deleted.
Public void Clean (date date) {
long diffrence;
Boolean Delete;
if (deque.size () ==0) {
return;
}
Delete=false;
do{
Event e=deque.getlast ();
diffrence=date.gettime ()-e.getdate (). GetTime ();
if (diffrence>10000) {
System.out.printf ("cleaner:%s\n", E.getevent ());
deque.removelast ();
delete=true;
}
}while (diffrence>10000);
if (delete) {
System.out.printf ("Cleaner:size of the The queue:%d\n", deque.size ());
}
}public void Clean (date date) {
long diffrence;
Boolean Delete;
if (deque.size () ==0) {
return;
}
Delete=false;
do{
Event e=deque.getlast ();
diffrence=date.gettime ()-e.getdate (). GetTime ();
if (diffrence>10000) {
System.out.printf ("cleaner:%s\n", E.getevent ());
deque.removelast ();
delete=true;
}
}while (diffrence>10000);
if (delete) {
System.out.printf ("Cleaner:size of the The queue:%d\n", deque.size ());
}
9. Create a main master class that contains the main () method.
public class Main {
public static void Main (string[] args) {}}
10. Create a Queue object deque, which is used to store events.
Deque<event> deque=new arraydeque<event> ();
11. Create three Writertask threads and a cleanertask thread, and start.
writertask writer=new writertask (deque);
for (int i=0;i<3;i++) {
thread thread=new Thread (writer);
}
cleanertask cleaner=new cleanertask (deque );
}
12. Run to view the results. The queue object is found to grow to 30, and then to the end of the program, the queue length remains at 27-30, and the
Creation and operation of the daemon thread for JAVA concurrent programming