Java multithreading-daemon

Source: Internet
Author: User
Tags garbage cleaner

Java multithreading-daemon

Daemon, as its name implies, is a process that runs silently in the background. when no other process runs on the JVM, the most typical column is

The garbage cleaner provided by JVM is used to clean up memory garbage when other programs are not running. Therefore, it has a strong uncertainty,

Because you don't know when it will run, and you don't know when it will run, it is suitable for a less important cleaning job or monitoring on the server.

Listen to work.

Below we will use the JVM daemon to write a small example. The implementation effect is as follows:

In this recipe, we will learn how to create a daemon thread developing an example with two threads; one user thread that writes events on a queue and a daemon one that cleans that queue, removing the events which were generated more than 10 seconds ago.


The queue here is a two-way list. The Code is as follows:


Package com. bird. concursey; import java. util. Date; import java. util. Deque; import java. util. concurrent. TimeUnit; public class WriterTask implements Runnable {public WriterTask (Deque
 
  
Deque) {this. deque = deque;} // this is a two-way queue private Deque
  
   
Deque; public Deque
   
    
GetDeque () {return deque;} public void setDeque (Deque
    
     
Deque) {this. deque = deque ;}@ Overridepublic void run () {for (int I = 0; I <100; I ++) {Event event = new Event (); event. setDate (new Date (); event. setEvent ("The Thread" + Thread. currentThread (). getId () + "has generated a event"); deque. addFirst (event); try {TimeUnit. SECONDS. sleep (1);} catch (InterruptedException e) {e. printStackTrace ();}}}}
    
   
  
 




Package com. bird. concursey; import java. util. Date; import java. util. Deque; public class CleanerTask extends Thread {private Deque
 
  
Deque; public CleanerTask (Deque
  
   
Deque) {this. deque = deque; // set it to the daemon (true);} @ Overridepublic void run () {while (true) {Date date Date = new Date (); clean (date) ;}} private void clean (Date date) {long difference = 0; boolean delete = false; if (deque. size () = 0) {return;} do {Event e = deque. getLast (); difference = date. getTime ()-e. getDate (). getTime (); 'if (difference> 10000) {System. out. println ("cleaner" + e. getEvent (); deque. removeLast (); delete = true ;}} while (difference> 10000); if (delete) {System. out. println ("cleaner: the size of the deque" + deque. size () ;}} public Deque
   
    
GetDeque () {return deque;} public void setDeque (Deque
    
     
Deque) {this. deque = deque ;}}
    
   
  
 

package com.bird.concursey;import java.util.ArrayDeque;import java.util.Date;import java.util.Deque;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;}public static void main(String[] args) {Deque
 
   deque = new ArrayDeque
  
   ();WriterTask writer = new WriterTask(deque);for(int i = 0; i < 3; i++) {Thread thread = new Thread(writer);thread.start();}CleanerTask cleaner = new CleanerTask(deque);cleaner.start();}}
  
 

The running result is

cleaner The Thread 12 has generated a eventcleaner The Thread 11 has generated a eventcleaner : the size of the deque 27cleaner The Thread 13 has generated a eventcleaner : the size of the deque 26cleaner The Thread 13 has generated a eventcleaner The Thread 12 has generated a eventcleaner The Thread 11 has generated a eventcleaner : the size of the deque 26cleaner The Thread 12 has generated a e

This queue is always maintained at around 25-30, because up to 30 events are created in three threads, and then the daemon line is waiting for the thread that creates the event to rest.

The process will take up the CPU time to run and clean up the established events, so that the queue will be maintained at around 25-30, the most critical sentence

// Set it to the daemon (true) daemon; it must be set before start, because once the thread is running, it cannot change its status.

After the monitoring status starts, the daemon process is OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.