Detailed explanation of Java thread properties

Introduction

This section introduces the attributes of threads in Java, namely the thread number (ID), name (Name), daemon thread (Daemon), and priority (Priority). matter.
insert image description here

1. Thread ID

The thread ID is used to identify different threads, and each thread ID is different and cannot be modified.

The thread IDs we create are not incremented from 2, because other threads are created when the JVM starts.

2. Thread name

Attributes set up to allow programmers to easily distinguish different threads and locate problems during development and debugging

This property will be the only thread property commonly used by programmers, other properties are not commonly used and should not even be used.

After the thread is created, the name can be changed, but after the thread is running, the name cannot be changed.

3. Guardian thread

The role of the daemon thread is to provide services to the user thread

How to set up a daemon thread

In setDaemon(true), true indicates a daemon thread, and false indicates a user thread. It must be valid before the thread runs, otherwise it is invalid

thread.setDaemon(true);
thread.start();

Is the main function a daemon thread?

No, the main function is a user thread.

Check whether the thread is a daemon thread in the Debug state of the thread

insert image description here

When does the daemon thread stop

After all user threads are stopped, the daemon thread will stop running along with the JVM.

For example, a garbage collection thread is a daemon thread, which provides services for user threads. When the JVM exits, the garbage collection thread will also stop running and no longer consume system resources.

Should threads be made daemon threads

在开发中,应该尽量避免使用守护线程,而使用用户线程,因为守护线程在JVM离开后,代码将不执行,此时会造成数据不一致问题。

4. Thread priority

The priority of Java threads is 10, the minimum is 1, the highest is 10, and the default is 5. When a thread is created, the priority automatically inherits the priority of the parent thread.

Java priority definition source code

/**
  * The minimum priority that a thread can have.
  */
 public final static int MIN_PRIORITY = 1;
/**
  * The default priority that is assigned to a thread.
  */
 public final static int NORM_PRIORITY = 5;
 /**
  * The maximum priority that a thread can have.
  */
 public final static int MAX_PRIORITY = 10;

Do I need to manually set thread priorities?

No, because the priority is determined by the system where the Java program is running, and it is not set to take effect. Different operating systems have different priorities and thread priorities. If Linux has no concept of priority, all threads have the same priority in Linux. But in some operating systems, the priority can be upgraded and downgraded, so when writing code, it is best not to specify the priority, just use the default.

Summarize

This article introduces the four attributes of a thread. In fact, in addition to the thread name attribute, other attributes do not need to be specified manually during the coding process. The thread ID, priority and daemon thread are used more as read-only properties and do not need to manually modify the default configuration.

Guess you like

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