JAVA thread Four: thread state check, background thread and thread group __java

Source: Internet
Author: User
Tags thread class

One, the thread state of the check

In general, you cannot determine the running state of a thread, and you can use the IsAlive () method to determine whether a thread is still active for those threads that are in an unknown state. Of course even an active thread does not mean that the thread must be running, and this method returns a value of true for a thread that has started running but has not yet completed the task.
Second, background thread

A background thread, or daemon thread, is a thread that performs a service in the background, such as a hidden thread in the operating system, an automatic garbage collection thread in Java, and so on. If all non-background threads end, the background thread will also terminate automatically. For example, a main function, main (), is a non-background thread.
You can use the Setdaemon () method in the thread class to set up a thread as a background thread, but it is worth noting that the Setdaemon () method must be called before the thread is started, so that it can be set as a background thread. When the setting completes a background thread, you can use the Isdaemon () method in the thread class to determine if the thread is a background thread.
For example:
public class Daemonthread extends Thread

{
Public Daemonthread ()

      {
             Setdaemon (True);                              //Set up background threads before thread start
              start ();                                                       /start Thread
       }
       public static void Main (string[] args)

{
Thread thread=new Daemonthread (); Instantiating the Thread object
Thread.isdaemon (); To determine if thread is a background thread
}
}
Code Description: Set the thread as a background thread in the constructor, then start, create the thread in the main function, and use the Isdaemon () method to determine if the thread is a background thread.

Third, Thread group

In Java, where each thread belongs to a member of a thread group management, such as generating a thread in the main function main () main workflow, the resulting thread belongs to the member of the thread group management of Main. Simply put, a thread group is a class of thread-managed threads, which is the Java.lang.ThreadGroup class.
You can obtain the name of the thread group that this thread belongs to by using the following code.
Thread.CurrentThread (). Getthreadgroup (). GetName ();
CurrentThread (): Gets the current thread.
Getthreadgroup (): Gets the group where the current thread is located.
GetName (): Gets the name of the group.
Define a thread group that can be implemented by using the following code.
Threadgroup group=new threadgroup ("group");
Thread thread=new thread (group, "the" "the" the "the");
Some methods in the Threadgroup class can have a effect on threads in a thread group. For example, the Setmaxpriority () method can set the maximum priority for all threads in a thread group.

Reference text: http://www.isstudy.com/java/1895.html

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.