Java thread (11): Thread Scheduling-daemon thread

Source: Internet
Author: User

Java thread: Thread Scheduling-what is the difference between the daemon thread and the normal thread Writing Method? To Call The thread object method setDaemon (true), you can set it as a daemon thread. Daemon threads are rarely used, but are not useless. For example, JVM garbage collection and memory management threads are all daemon threads. There is also the database connection pool used for database applications. The connection pool also contains many background threads to monitor the number of connections, timeout time, status, and so on. SetDaemon: public final void setDaemon (boolean on) marks this thread as a daemon thread or user thread. When all running threads are daemon threads, the Java Virtual Machine exits. This method must be called before the thread is started. This method first calls the checkAccess method of the thread without any parameters. This may throw a SecurityException (in the current thread ). Parameter: on-if it is true, the thread is marked as a daemon thread. Throw: IllegalThreadStateException-if the thread is active. SecurityException-if the current thread cannot be modified. For more information, see isDaemon (), checkAccess ()/*** Java thread: thread Scheduling-daemon Thread ** @ author leizhimin 9:02:40 */public class Test {public static void main (String [] args) {Thread t1 = new MyCommon (); thread t2 = new Thread (new MyDaemon (); t2.setDaemon (true); // set it to the daemon Thread t2.start (); t1.start ();}} class MyCommon extends Thread {public void run () {for (int I = 0; I <5; I ++) {System. out. println ("thread 1" + I + "execution! "); Try {Thread. sleep (7);} catch (InterruptedException e) {e. printStackTrace () ;}}} class MyDaemon implements Runnable {public void run () {for (long I = 0; I <9999999L; I ++) {System. out. println ("background Thread No." + I + "times! "); Try {Thread. sleep (7);} catch (InterruptedException e) {e. printStackTrace () ;}}} the background Thread executes 0th times! Thread 1 executes 0th times! Thread 1 executes 1st times! The background thread executes 1st times! The background thread executes 2nd times! Thread 1 executes 2nd times! Thread 1 executes 3rd Times! The background thread executes 3rd Times! Thread 1 executes 4th times! The background thread executes 4th times! The background thread executes 5th times! The background thread executes 6th times! The background thread executes 7th times! Process finished with exit code 0 can be seen from the preceding execution results: the foreground thread ensures that the execution is complete, and the background thread exits after the execution is complete. In fact, the standard for JRE to determine whether the program is executed is that all the frontend thread lines are completed, regardless of the background thread status. Therefore, you must pay attention to this issue when using the background county seat.

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.