java线程基础巩固---线程ID,优先级
这里学习Thread的两个比较简单的API,直接上代码:
线程ID:
那它的生成规则是?直接看源码:
那为什么目前打印是9呢?然后在jvm启动的时候就已经创建了8个线程?继续用jconsole来验证一下:
启动jconsole:
优先级:
我们在学习java线程中都知道这个优先级在实际中是不受控的,也就是说要控制线程的优先级不能通过它的API来,而应该是从咱们的逻辑上去实现,但是这里还是做一个了解,试一下它的效果:
编译运行:
Thread-0-index0
Thread-0-index1
Thread-0-index2
Thread-0-index3
Thread-0-index4
Thread-0-index5
Thread-0-index6
Thread-0-index7
Thread-0-index8
Thread-0-index9
Thread-1-index0
Thread-1-index1
Thread-1-index2
Thread-0-index10
Thread-1-index3
Thread-1-index4
Thread-1-index5
Thread-1-index6
Thread-0-index11
Thread-2-index0
Thread-2-index1
Thread-2-index2
Thread-2-index3
Thread-1-index7
Thread-1-index8
Thread-1-index9
Thread-2-index4
Thread-2-index5
Thread-2-index6
Thread-0-index12
Thread-0-index13
Thread-0-index14
Thread-2-index7
Thread-1-index10
Thread-1-index11
Thread-1-index12
Thread-1-index13
Thread-1-index14
Thread-1-index15
Thread-1-index16
Thread-2-index8
Thread-2-index9
Thread-0-index15
Thread-2-index10
Thread-2-index11
Thread-2-index12
Thread-2-index13
Thread-2-index14
Thread-2-index15
Thread-2-index16
Thread-2-index17
Thread-2-index18
Thread-2-index19
Thread-2-index20
Thread-2-index21
Thread-2-index22
Thread-2-index23
Thread-2-index24
Thread-1-index17
Thread-2-index25
Thread-0-index16
Thread-2-index26
Thread-1-index18
Thread-2-index27
Thread-0-index17
Thread-2-index28
Thread-2-index29
Thread-2-index30
Thread-2-index31
Thread-2-index32
Thread-2-index33
Thread-2-index34
Thread-2-index35
Thread-2-index36
Thread-2-index37
Thread-1-index19
Thread-2-index38
Thread-2-index39
Thread-2-index40
Thread-2-index41
Thread-2-index42
Thread-2-index43
Thread-2-index44
Thread-0-index18
Thread-2-index45
Thread-2-index46
Thread-1-index20
Thread-1-index21
Thread-2-index47
Thread-0-index19
Thread-2-index48
Thread-1-index22
Thread-2-index49
Thread-2-index50
Thread-2-index51
Thread-2-index52
Thread-0-index20
Thread-2-index53
Thread-1-index23
Thread-1-index24
Thread-2-index54
Thread-0-index21
Thread-2-index55
Thread-2-index56
Thread-1-index25
Thread-2-index57
Thread-2-index58
Thread-2-index59
Thread-2-index60
Thread-2-index61
Thread-2-index62
Thread-2-index63
Thread-2-index64
Thread-2-index65
Thread-2-index66
Thread-2-index67
Thread-2-index68
Thread-0-index22
Thread-0-index23
Thread-0-index24
Thread-0-index25
Thread-2-index69
Thread-1-index26
Thread-1-index27
Thread-1-index28
Thread-2-index70
Thread-2-index71
Thread-2-index72
Thread-0-index26
Thread-2-index73
Thread-1-index29
Thread-1-index30
Thread-1-index31
Thread-1-index32
Thread-1-index33
Thread-1-index34
Thread-2-index74
Thread-0-index27
Thread-0-index28
Thread-0-index29
Thread-2-index75
Thread-1-index35
Thread-2-index76
Thread-0-index30
Thread-0-index31
Thread-0-index32
Thread-2-index77
Thread-2-index78
Thread-2-index79
Thread-2-index80
Thread-2-index81
Thread-2-index82
Thread-2-index83
Thread-2-index84
Thread-2-index85
Thread-1-index36
Thread-2-index86
Thread-0-index33
Thread-0-index34
Thread-0-index35
Thread-0-index36
Thread-0-index37
Thread-0-index38
Thread-0-index39
Thread-0-index40
Thread-0-index41
Thread-0-index42
Thread-0-index43
Thread-0-index44
Thread-0-index45
Thread-2-index87
Thread-1-index37
Thread-1-index38
Thread-2-index88
Thread-0-index46
Thread-0-index47
Thread-0-index48
Thread-0-index49
Thread-0-index50
Thread-0-index51
Thread-0-index52
Thread-0-index53
Thread-0-index54
Thread-2-index89
Thread-1-index39
Thread-1-index40
Thread-1-index41
Thread-1-index42
Thread-1-index43
Thread-2-index90
Thread-2-index91
Thread-2-index92
Thread-0-index55
Thread-2-index93
Thread-2-index94
Thread-1-index44
Thread-2-index95
Thread-2-index96
Thread-2-index97
Thread-2-index98
Thread-2-index99
Thread-0-index56
Thread-1-index45
Thread-1-index46
Thread-1-index47
Thread-1-index48
Thread-1-index49
Thread-1-index50
Thread-1-index51
Thread-1-index52
Thread-1-index53
Thread-1-index54
Thread-1-index55
Thread-1-index56
Thread-1-index57
Thread-1-index58
Thread-1-index59
Thread-0-index57
Thread-1-index60
Thread-1-index61
Thread-1-index62
Thread-1-index63
Thread-1-index64
Thread-1-index65
Thread-1-index66
Thread-1-index67
Thread-1-index68
Thread-1-index69
Thread-1-index70
Thread-1-index71
Thread-1-index72
Thread-1-index73
Thread-1-index74
Thread-1-index75
Thread-1-index76
Thread-1-index77
Thread-1-index78
Thread-1-index79
Thread-1-index80
Thread-1-index81
Thread-1-index82
Thread-1-index83
Thread-1-index84
Thread-1-index85
Thread-1-index86
Thread-1-index87
Thread-1-index88
Thread-1-index89
Thread-1-index90
Thread-0-index58
Thread-1-index91
Thread-0-index59
Thread-0-index60
Thread-1-index92
Thread-0-index61
Thread-0-index62
Thread-0-index63
Thread-1-index93
Thread-0-index64
Thread-1-index94
Thread-0-index65
Thread-0-index66
Thread-0-index67
Thread-0-index68
Thread-0-index69
Thread-1-index95
Thread-0-index70
Thread-0-index71
Thread-0-index72
Thread-0-index73
Thread-0-index74
Thread-0-index75
Thread-1-index96
Thread-0-index76
Thread-0-index77
Thread-1-index97
Thread-0-index78
Thread-1-index98
Thread-1-index99
Thread-0-index79
Thread-0-index80
Thread-0-index81
Thread-0-index82
Thread-0-index83
Thread-0-index84
Thread-0-index85
Thread-0-index86
Thread-0-index87
Thread-0-index88
Thread-0-index89
Thread-0-index90
Thread-0-index91
Thread-0-index92
Thread-0-index93
Thread-0-index94
Thread-0-index95
Thread-0-index96
Thread-0-index97
Thread-0-index98
Thread-0-index99
当然每次执行可能不一样,这时设置一下各线程中的优先级:
编译运行:
Thread-0-index0
Thread-0-index1
Thread-0-index2
Thread-0-index3
Thread-0-index4
Thread-0-index5
Thread-0-index6
Thread-0-index7
Thread-0-index8
Thread-0-index9
Thread-0-index10
Thread-0-index11
Thread-0-index12
Thread-0-index13
Thread-0-index14
Thread-0-index15
Thread-0-index16
Thread-0-index17
Thread-0-index18
Thread-0-index19
Thread-0-index20
Thread-0-index21
Thread-0-index22
Thread-0-index23
Thread-0-index24
Thread-0-index25
Thread-0-index26
Thread-0-index27
Thread-0-index28
Thread-0-index29
Thread-0-index30
Thread-0-index31
Thread-1-index0
Thread-1-index1
Thread-0-index32
Thread-1-index2
Thread-1-index3
Thread-1-index4
Thread-0-index33
Thread-1-index5
Thread-0-index34
Thread-1-index6
Thread-1-index7
Thread-1-index8
Thread-1-index9
Thread-0-index35
Thread-1-index10
Thread-2-index0
Thread-2-index1
Thread-1-index11
Thread-0-index36
Thread-0-index37
Thread-0-index38
Thread-0-index39
Thread-1-index12
Thread-1-index13
Thread-1-index14
Thread-2-index2
Thread-1-index15
Thread-1-index16
Thread-1-index17
Thread-1-index18
Thread-1-index19
Thread-1-index20
Thread-0-index40
Thread-1-index21
Thread-2-index3
Thread-1-index22
Thread-0-index41
Thread-1-index23
Thread-2-index4
Thread-1-index24
Thread-1-index25
Thread-1-index26
Thread-1-index27
Thread-0-index42
Thread-1-index28
Thread-2-index5
Thread-2-index6
Thread-1-index29
Thread-1-index30
Thread-0-index43
Thread-0-index44
Thread-0-index45
Thread-0-index46
Thread-0-index47
Thread-1-index31
Thread-2-index7
Thread-2-index8
Thread-1-index32
Thread-0-index48
Thread-1-index33
Thread-2-index9
Thread-1-index34
Thread-0-index49
Thread-1-index35
Thread-1-index36
Thread-1-index37
Thread-1-index38
Thread-1-index39
Thread-1-index40
Thread-1-index41
Thread-1-index42
Thread-1-index43
Thread-1-index44
Thread-1-index45
Thread-1-index46
Thread-1-index47
Thread-1-index48
Thread-1-index49
Thread-1-index50
Thread-1-index51
Thread-1-index52
Thread-1-index53
Thread-1-index54
Thread-1-index55
Thread-1-index56
Thread-1-index57
Thread-1-index58
Thread-1-index59
Thread-1-index60
Thread-1-index61
Thread-1-index62
Thread-1-index63
Thread-1-index64
Thread-1-index65
Thread-1-index66
Thread-1-index67
Thread-1-index68
Thread-1-index69
Thread-1-index70
Thread-1-index71
Thread-1-index72
Thread-1-index73
Thread-1-index74
Thread-1-index75
Thread-1-index76
Thread-1-index77
Thread-1-index78
Thread-1-index79
Thread-1-index80
Thread-1-index81
Thread-1-index82
Thread-1-index83
Thread-1-index84
Thread-1-index85
Thread-1-index86
Thread-1-index87
Thread-1-index88
Thread-1-index89
Thread-1-index90
Thread-1-index91
Thread-1-index92
Thread-1-index93
Thread-1-index94
Thread-1-index95
Thread-1-index96
Thread-1-index97
Thread-1-index98
Thread-1-index99
Thread-2-index10
Thread-0-index50
Thread-2-index11
Thread-0-index51
Thread-2-index12
Thread-0-index52
Thread-2-index13
Thread-2-index14
Thread-2-index15
Thread-0-index53
Thread-2-index16
Thread-0-index54
Thread-0-index55
Thread-0-index56
Thread-0-index57
Thread-0-index58
Thread-2-index17
Thread-0-index59
Thread-0-index60
Thread-2-index18
Thread-0-index61
Thread-2-index19
Thread-0-index62
Thread-2-index20
Thread-0-index63
Thread-2-index21
Thread-0-index64
Thread-2-index22
Thread-0-index65
Thread-0-index66
Thread-0-index67
Thread-0-index68
Thread-0-index69
Thread-0-index70
Thread-0-index71
Thread-0-index72
Thread-0-index73
Thread-0-index74
Thread-0-index75
Thread-0-index76
Thread-0-index77
Thread-0-index78
Thread-0-index79
Thread-0-index80
Thread-0-index81
Thread-0-index82
Thread-0-index83
Thread-0-index84
Thread-0-index85
Thread-0-index86
Thread-0-index87
Thread-0-index88
Thread-0-index89
Thread-0-index90
Thread-0-index91
Thread-0-index92
Thread-0-index93
Thread-0-index94
Thread-0-index95
Thread-0-index96
Thread-0-index97
Thread-0-index98
Thread-0-index99
Thread-2-index23
Thread-2-index24
Thread-2-index25
Thread-2-index26
Thread-2-index27
Thread-2-index28
Thread-2-index29
Thread-2-index30
Thread-2-index31
Thread-2-index32
Thread-2-index33
Thread-2-index34
Thread-2-index35
Thread-2-index36
Thread-2-index37
Thread-2-index38
Thread-2-index39
Thread-2-index40
Thread-2-index41
Thread-2-index42
Thread-2-index43
Thread-2-index44
Thread-2-index45
Thread-2-index46
Thread-2-index47
Thread-2-index48
Thread-2-index49
Thread-2-index50
Thread-2-index51
Thread-2-index52
Thread-2-index53
Thread-2-index54
Thread-2-index55
Thread-2-index56
Thread-2-index57
Thread-2-index58
Thread-2-index59
Thread-2-index60
Thread-2-index61
Thread-2-index62
Thread-2-index63
Thread-2-index64
Thread-2-index65
Thread-2-index66
Thread-2-index67
Thread-2-index68
Thread-2-index69
Thread-2-index70
Thread-2-index71
Thread-2-index72
Thread-2-index73
Thread-2-index74
Thread-2-index75
Thread-2-index76
Thread-2-index77
Thread-2-index78
Thread-2-index79
Thread-2-index80
Thread-2-index81
Thread-2-index82
Thread-2-index83
Thread-2-index84
Thread-2-index85
Thread-2-index86
Thread-2-index87
Thread-2-index88
Thread-2-index89
Thread-2-index90
Thread-2-index91
Thread-2-index92
Thread-2-index93
Thread-2-index94
Thread-2-index95
Thread-2-index96
Thread-2-index97
Thread-2-index98
Thread-2-index99
从结果中确实有一些影响,第一个线程先执行了,第二个次之,第三个最后执行,但是基本也是交替进行的,所以这里纯学习试验,实际基本上不会用这个API,了解下。
java线程基础巩固---线程ID,优先级的更多相关文章
- Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量
Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量 一丶线程的理论知识 什么是线程: 1.线程是一堆指令,是操作系统调度 ...
- java线程基础巩固---线程生命周期以及start方法源码剖析
上篇中介绍了如何启动一个线程,通过调用start()方法才能创建并使用新线程,并且这个start()是非阻塞的,调用之后立马就返回的,实际上它是线程生命周期环节中的一种,所以这里阐述一下线程的一个完整 ...
- Java并发基础:线程的创建
线程的创建和管理: 1.应用Thread类显式创建.管理线程 2.应用Executor创建并管理线程. 定义任务: 无返回的任务:实现Runnable接口并编写run()方法. 有响应的任务:实现Ca ...
- java线程基础巩固---线程生产者消费者的综合实战结合Java8语法
基于上一次[http://www.cnblogs.com/webor2006/p/8909558.html]学习的多个生产者与多个消费者模型,此次用另外一个案例来进一步巩固线程之间的调度处理,这里还是 ...
- 014-多线程-基础-Exchanger-行线程间的数据交换
一.简介 Exchanger类允许在两个线程之间定义同步点,当两个线程都到达同步点时,它们交换数据.也就是第一个线程的数据进入到第二个线程中,第二线程的数据进入到第一个线程中. Exchanger可以 ...
- java线程基础知识----线程基础知识
不知道从什么时候开始,学习知识变成了一个短期记忆的过程,总是容易忘记自己当初学懂的知识(fuck!),不知道是自己没有经常使用还是当初理解的不够深入.今天准备再对java的线程进行一下系统的学习,希望 ...
- java线程基础知识----线程与锁
我们上一章已经谈到java线程的基础知识,我们学习了Thread的基础知识,今天我们开始学习java线程和锁. 1. 首先我们应该了解一下Object类的一些性质以其方法,首先我们知道Object类的 ...
- java线程基础巩固---线程间通信快速入门,使用wait和notify进行线程间的数据通信
之前已经对于线程同步相关的知识点进行了详细的学习,这次来学习一下线程间的通信相关的知识,话不多说直接用代码进行演练,以一个简陋的生产者消费者模型来初步了解下线程间通信是怎么一回事. 生产消费者第一版: ...
- Java并发基础06. 线程范围内共享数据
假设现在有个公共的变量 data,有不同的线程都可以去操作它,如果在不同的线程对 data 操作完成后再去取这个 data,那么肯定会出现线程间的数据混乱问题,因为 A 线程在取 data 数据前可能 ...
随机推荐
- 一些php常用函数积累
本文链接 <?php // id: ecffe70d3af54df9bad97b61918ace7d global $ct_path, $ct_log_path; $log_path = &qu ...
- ASP.NET Core 入门笔记8,ASP.NET Core MVC 分部视图入门
一.前言 1.本教程主要内容 ASP.NET Core MVC (Razor)分部视图简介 ASP.NET Core MVC (Razor)分部视图基础教程 ASP.NET Core MVC (Raz ...
- Linux远程开发
Linux远程开发 通常,当我们开发Linux程序时有两种方案: 在Linux上直接编写程序并进行运行测试和调试 在Windows或Mac OS X上借助工具进行远程开发 虽然我自己是在Linux环境 ...
- 使用批处理执行 sql cmd
当 sql脚本文件太大了, 就可以考虑用这个方式来执行 1.准备好要执行的 sql脚本文件并保存为*.sql格式 2.找到 SQLCMD.EXE 文件所在的路径(每个版本sql路径不一样) C:\Pr ...
- windows环境jar包部署到linux服务器,一键操作
背景: windows系统下生成的jar包通过FTP上传到linux服务器,然后通过XShell进行jar包的发布,这样反复了几个月后,开发阶段需要频繁更新包的部署.个人觉得很繁琐,想一键式把这个工作 ...
- length 和 size 区分
总是混淆length和size,今天专门区分一下 1.在java代码(.java)中 1.length属性是针对Java中的数组来说的,要求数组的长度可以用其length属性: 2.length( ...
- uname、hostname命令
一.uname:显示系统信息. 语法: uname [OPTION] ... 描述 打印某些系统信息. 没有选项,与-s相同. -a,--all ...
- Java源码 HashMap<K,V>
HashMap类 https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html public class HashMap<K, ...
- Java 封装与类
一.面向对象编程 面向对象编程三大特性:封装.继承和多态. 类是实现封装的手段,是面向对象编程的基本单元. 封装隐藏了类的内部实现细节,暴露给外界可控的操作,提高数据的完整性和安全性,提高模块的可重用 ...
- IntelliJ IDEA Spring boot devtools 实现热部署
一.spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动部署新代码. 二.原理 使用了两个ClassLoader,一个ClassLoader用来加载那些不会变 ...