Java background process and thread priority

Source: Internet
Author: User

1. Background thread: The task is running in the background and serves other threads. It is also called "daemon thread" or "Genie thread ". JVM garbage collection is a typical background thread. Features: If all foreground threads die, background threads automatically die. Set background Thread: the Thread object setDaemon (true); setDaemon (true) must be before start. Otherwise, an exception occurs in IllegalThreadStateException. The Thread created by the foreground Thread is the foreground Thread by default. Determine whether the Thread is a background Thread: Use the isDaemon () method of the Thread object; the new thread is the background thread only when the creation thread is a background thread. Example: class Daemon implements Runnable {public void run () {for (int I = 0; I <200; I ++) {System. out. println ("Daemon -->" + I) ;}} public class DaemonDemo {public static void main (String [] args) {/* Thread cThread = Thread. currentThread (); System. out. println (cThread. isAlive (); // cThread. setDaemon (true); System. out. println (cThread. isDaemon (); */Thread t = new Thread (new Daemon (); System. out. println (T. isDaemon (); for (int I = 0; I <10; I ++) {System. out. println ("main --" + I); if (I = 5) {t. setDaemon (true); t. start () ;}}} 2. thread priority: Each thread has a priority. The priority is only related to the number of times the thread gets the execution opportunity. The higher the thread priority, it must be executed first. The first running of a thread depends on the CPU scheduling. By default, the main thread has a normal priority, and the thread it creates has a normal priority. SetPriority (int x) and getPriority () of the Thread object to set and obtain the priority. MAX_PRIORITY: The value is 10MIN_PRIORITY: The value is 1NORM_PRIORITY: The value is 5 (the default priority of the main method). Note: The default priority of each thread is the same as that of the parent thread created, by default, the main thread has a normal Priority. The subthread created by the main thread also has a normal Priority. Example: class Priority implements Runnable {public void run () {for (int I = 0; I <200; I ++) {System. out. println ("Priority --" + I) ;}} public class PriorityDemo {public static void main (String [] args) {/*** the Thread priority is between [1, 10] */Thread. currentThread (). setPriority (3); System. out. println ("main =" + Thread. currentThread (). getPriority ();/** public final static int MIN_PRIORITY = 1; * public final static int NORM_PRIORITY = 5; * public final static int MAX_PRIORITY = 10; **/System. out. println (Thread. MAX_PRIORITY ); // ================================================ ========= Thread t = new Thread (new Priority ()); for (int I = 0; I <200; I ++) {System. out. println ("main" + I); if (I = 50) {t. start (); t. setPriority (10);} System. out. println ("-------------------------" + t. getPriority ());}}}

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.