Java multithreading - thread scheduling (daemon thread)

The daemon thread is basically the same as the ordinary thread writing method. You can set it as a daemon thread by calling the method setDaemon(true) of the thread object.

Daemon threads are rarely used, but not useless. For example, JVM garbage collection, memory management and other threads are daemon threads. There is also the database connection pool used when doing database applications. The connection pool itself also contains many background threads to monitor the number of connections, timeout, status, and so on.

Detailed description of the setDaemon method:
public final void setDaemon(boolean on): Mark the thread as a daemon thread or a user thread. When the running threads are all daemon threads, the Java virtual machine exits.

This method must be called before starting the thread.

The method first calls the thread's checkAccess method without any parameters. This may throw a SecurityException (in the current thread).


Parameters: 
on - if true, marks the thread as a daemon thread. 
Throws: 
IllegalThreadStateException - if the thread is active. 
SecurityException - if the current thread cannot modify the thread. 
See also: 
isDaemon(), checkAccess()

copy code
package cn.thread;

/**
 * Thread scheduling (daemon thread)
 *
 * @author Lin Jiqin
 * @version 1.0 2013-7-24 09:30:42 AM
  */ 
public  class ThreadDaemon {
    
    public static void main(String[] args) {
           ThreadDaemon thread=new ThreadDaemon();
           Thread t1 = thread.new MyThread1(); 
           Thread t2 = new Thread(thread.new MyRunnable()); 
           t2.setDaemon( true ); // Set as a daemon thread
           
           t2.start();
           t1.start();
    }
    
    class MyThread1 extends Thread {
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println( "Thread 1" + i + "execution!" );
                 try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        }
    }

    class MyRunnable implements Runnable {
        public void run() {
            for (long i = 0; i < 9999999L; i++) { 
                System.out.println( "Background thread" + i + "execution!" ); 
                 try {
                        Thread.sleep(7); 
                } catch (InterruptedException e) { 
                        e.printStackTrace ();
                }
        }
        }
    }
}
copy code
copy code
The 0th execution of the background thread!
Thread 1 executes for the 0th time!
The first execution of the background thread!
The background thread is executed for the second time!
The background thread is executed for the 3rd time!
The 4th execution of the background thread!
The 5th execution of the background thread!
The 6th execution of the background thread!
The 7th execution of the background thread!
The 8th execution of the background thread!
The 9th execution of the background thread!
The 10th execution of the background thread!
The 11th execution of the background thread!
The 12th execution of the background thread!
Thread 1 executes for the first time!
The 13th execution of the background thread!
The 14th execution of the background thread!
The 15th execution of the background thread!
The 16th execution of the background thread!
The 17th execution of the background thread!
The 18th execution of the background thread!
The 19th execution of the background thread!
The 20th execution of the background thread!
The 21st execution of the background thread!
The 22nd execution of the background thread!
The 23rd execution of the background thread!
The 24th execution of the background thread!
The 25th execution of the background thread!
Thread 1 executes for the second time!
The 26th execution of the background thread!
The 27th execution of the background thread!
The 28th execution of the background thread!
The 29th execution of the background thread!
The 30th execution of the background thread!
The 31st execution of the background thread!
The 32nd execution of the background thread!
The 33rd execution of the background thread!
The 34th execution of the background thread!
The 35th execution of the background thread!
The 36th execution of the background thread!
The 37th execution of the background thread!
The 38th execution of the background thread!
Thread 1 executes for the 3rd time!
The 39th execution of the background thread!
The 40th execution of the background thread!
The 41st execution of the background thread!
The 42nd execution of the background thread!
The 43rd execution of the background thread!
The 44th execution of the background thread!
The 45th execution of the background thread!
The 46th execution of the background thread!
The 47th execution of the background thread!
The 48th execution of the background thread!
The 49th execution of the background thread!
The 50th execution of the background thread!
The 51st execution of the background thread!
Thread 1 executes for the 4th time!
The 52nd execution of the background thread!
The 53rd execution of the background thread!
The 54th execution of the background thread!
The 55th execution of the background thread!
The 56th execution of the background thread!
The 57th execution of the background thread!
The 58th execution of the background thread!
The 59th execution of the background thread!
The 60th execution of the background thread!
The 61st execution of the background thread!
The 62nd execution of the background thread!
The 63rd execution of the background thread!
The 64th execution of the background thread!
The 65th execution of the background thread!
The 66th execution of the background thread!
The 67th execution of the background thread!
The 68th execution of the background thread!
The 69th execution of the background thread!
The 70th execution of the background thread!
copy code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326249784&siteId=291194637