Test daemon Thread java-verbose view JDK installation path

Source: Internet
Author: User

Test daemon thread, 1. Creates a thread using an inner class to create thread D, which implements the output string "background thread" every 0.1 seconds. 2. Set thread d as the daemon thread and start the thread. 3. Block the main thread for 5 seconds, and then output the string "main thread ended."

One, the creation thread, implements every 0.1 seconds output string "Background thread", first creates the new class Testdaemonthread, then in this class's Main method, uses the inner class to create the thread the way to create the thread D; the last thread D implements the output string "background thread" every 0.1 seconds.

Second, set the D thread as a background thread, in the main method, first set the D thread as background Xian Chen and start the thread, and then use the thread class's sleep method to block the main thread for 5 seconds; the last output string "Main thread ended"

Import Java.awt.Color;

Import Javax.swing.JFrame;
Import Javax.swing.JPanel;

Get the thread name and ID
public class Testdaemonthread {
GetName method and GetID method for testing thread
public static void Main (string[] args) {
Thread d = new Thread () {
public void Run () {
while (true) {
SYSTEM.OUT.PRINTLN ("Background thread");
try{
Thread.Sleep (100);
}catch (Interruptedexception e) {

}
}
}
};
D.setdaemon (TRUE);
D.start ();
try{
Thread.Sleep (5000);
}catch (Interruptedexception e) {
}
After all foreground threads in the process end, the background thread forces the end
System.out.println ("Main thread ended");
}
}

Running the Testdaemonthread class, the console will continuously output the string "background thread" until the output string "Main thread is finished". This is because the D thread is set as the daemon thread, and the daemon thread is characterized by the forced termination of all daemons when only the daemon thread is left in the process.

  

Test daemon Thread java-verbose view JDK installation path

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.