File tree Expand file tree Collapse file tree 7 files changed +103
-0
lines changed
Thread/src/threadattributes Expand file tree Collapse file tree 7 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 88 正确停止线程
99 线程六大生命周期
1010 Thread和Object中的重要方法
11+ 线程的属性
1112 ThreadPool
1213 ThreadLocal
1314
Original file line number Diff line number Diff line change 1+ package threadattributes ;
2+
3+ /**
4+ * 线程属性总结
5+ *
6+ * @Author: zzStar
7+ * @Date: 10-18-2020 13:32
8+ */
9+ public class AttributesSummary {
10+ /**
11+ * ID NAME isDaemon Priority
12+ */
13+ }
Original file line number Diff line number Diff line change 1+ package threadattributes ;
2+
3+ /**
4+ * 守护线程 -> 给用户线程提供服务
5+ *
6+ * @Author: zzStar
7+ * @Date: 10-18-2020 15:37
8+ */
9+ public class DaemonThread {
10+
11+ /**
12+ * 1.线程默认继承自父线程
13+ * 2.通常,守护线程都是由JVM自动启动
14+ * 3.不影响JVM的退出
15+ */
16+
17+ /**
18+ * 和普通线程的区别 ->
19+ * 整体无区别
20+ * 唯一的区别在于是否影响JVM的退出
21+ */
22+
23+ /**
24+ * 不应该把线程设置为守护线程,因为JVM可能退出,导致操作过程中间被强行停止,导致数据不一致
25+ */
26+ }
Original file line number Diff line number Diff line change 1+ package threadattributes ;
2+
3+ /**
4+ * 注意 ❗ Id 是从 1 开始,JVM运行起来之后,自己创建的线程ID早已不是2
5+ * 特殊的一点
6+ *
7+ * @Author: zzStar
8+ * @Date: 10-18-2020 13:34
9+ */
10+ public class Id {
11+ public static void main (String [] args ) {
12+ Thread thread = new Thread ();
13+
14+ System .out .println ("主线程的ID" + Thread .currentThread ().getId ());//1
15+ System .out .println ("子线程的ID" + thread .getId ());//14
16+ }
17+ }
18+
19+ /*原因如下源码 -> 先++再返回
20+ private static synchronized long nextThreadID() {
21+ return ++threadSeqNumber;
22+ }
23+ */
24+
25+ /* 而这是线程名字的源码实现 -> 从0开始
26+ private static synchronized int nextThreadNum() {
27+ return threadInitNumber++;
28+ }
29+ */
30+
31+
Original file line number Diff line number Diff line change 1+ package threadattributes ;
2+
3+ /**
4+ * 线程的优先级
5+ *
6+ * @Author: zzStar
7+ * @Date: 10-18-2020 15:48
8+ */
9+ public class ThreadPriority {
10+ /**
11+ * 线程一共10个优先级
12+ * -> 默认是5
13+ * -> 程序设计不应依赖于优先级,不同操作系统不一样,优先级会被操作系统改变
14+ * 操作系统对优先级的映射和调度不一样,甚至可能被忽略
15+ */
16+
17+ /**
18+ * The minimum priority that a thread can have.
19+ */
20+ public static final int MIN_PRIORITY = 1 ;
21+
22+ /**
23+ * The default priority that is assigned to a thread.
24+ */
25+ public static final int NORM_PRIORITY = 5 ;
26+
27+ /**
28+ * The maximum priority that a thread can have.
29+ */
30+ public static final int MAX_PRIORITY = 10 ;
31+
32+ }
You can’t perform that action at this time.
0 commit comments