> For the complete documentation index, see [llms.txt](https://yibingxiong.gitbook.io/java-concurrent-programming-art-mini/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yibingxiong.gitbook.io/java-concurrent-programming-art-mini/di-02-zhang-bing-fa-bian-cheng-ji-chu/03-xian-cheng-de-yi-xie-shu-xing.md).

# 03-线程的一些属性

## 名字

给以给线程取一个响亮的名字，便于排查问题，默认为`Thread-${一个数字}`这个样子

* 设置名字

```java
threadA.setName("欢迎关注微信公号'大雄和你一起学编程'");
```

* 获取名字

```java
threadA.getName();
```

## 是否是守护线程(daemon)

为其他线程服务的线程可以是守护线程，守护线程的特点是如果所有的前台线程死亡，则守护线程自动死亡。

非守护线程创建的线程默认为非守护线程，守护则默认为守护

* set

```java
threadA.setDaemon(true);
```

* get

```java
threadA.isDaemon();
```

## 线程优先级(priority)

优先级高的线程可以得到更多cpu资源, 级别是1-10，默认优先级和创建他的父线程相同

### set

```java
threadA.setPriority(Thread.NORM_PRIORITY);
```

### get

```java
threadA.getPriority()
```

## 所属线程组

可以把线程放到组里，一起管理

### 设置线程组

Thread的构造里边可以指定

```java
ThreadGroup threadGroup = new ThreadGroup("欢迎关注微信公号'大雄和你一起学编程'");
Thread thread = new Thread(threadGroup, () -> {
    System.out.println("欢迎关注微信公号'大雄和你一起学编程'");
});
```

### 拿到线程组

```
thread.getThreadGroup()
```

### 基于线程组的操作

```java
ThreadGroup threadGroup1 = thread.getThreadGroup();
System.out.println(threadGroup1.activeCount()); // 有多少活的线程
threadGroup1.interrupt();                       // 中断组里所有线程
threadGroup1.setMaxPriority(10);                // 设置线程最高优先级是多少
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yibingxiong.gitbook.io/java-concurrent-programming-art-mini/di-02-zhang-bing-fa-bian-cheng-ji-chu/03-xian-cheng-de-yi-xie-shu-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
