当前位置:网站首页>线程调度的常用方法
线程调度的常用方法
2022-06-24 09:43:00 【文丑颜不良啊】
线程的相关方法基本可以分为以下几类:
| 类别 | 方法 |
|---|---|
| 等待 | wait() |
| wait(long timeout) | |
| wait(long timeout, int nanos) | |
| join() | |
| join(long timeout) | |
| join(long timeout, int nanos) | |
| 通知(唤醒) | notify() |
| notifyAll() | |
| 让出优先权 | yield() |
| 中断 | interrupt() |
| interruptted() | |
| isInterrupted() | |
| 休眠 | sleep() |
wait():当一个线程 A 调用一个共享变量的 wait() 方法时,线程 A 会被阻塞挂起, 发生下面两种情况才会返回 :(1)其它线程调用了共享对象的 notify() 或者 notifyAll() 方法;(2)其他线程调用了线程 A 的 interrupt() 方法,线程 A 抛出 InterruptedException 异常返回。
wait(long timeout):这个方法相比 wait() 方法多了一个超时参数,它的不同之处在于,如果线程 A 调用共享对象的 wait(long timeout) 方法后,没有在指定的 timeout 时间(单位:毫秒)内被其它线程唤醒,那么这个方法还是会因为超时而返回。
wait(long timeout, int nanos):其内部调用的是 wait(long timeout) 方法。源码为:
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}以上这三种方法是线程等待的方法,这三个方法都是 Object 类中的。
notify():一个线程 A 调用共享对象的 notify() 方法后,会唤醒一个在这个共享变量上调用 wait 系列方法后被挂起的线程。一个共享变量上可能会有多个线程在等待,具体唤醒哪个等待的线程是随机的。
notifyAll():不同于在共享变量上调用 notify() 方法会唤醒被阻塞到该共享变量上的一个线程,notifyAll() 方法则会唤醒所有在该共享变量上由于调用 wait 系列方法而被挂起的线程。
这两个方法都是 Object 类中提供的,除了这两个方法之外,Thread 类也提供了一系列线程唤醒的方法。
join()、join(long timeout)、join(long timeout, int nanaos):如果一个线程 A 执行了 thread.join() 语句,其含义是:当前线程 A 等待 thread 线程终止之后才会从 thread.join() 返回。join() 就相当于 join(0),join(log timeout, int nanos) 内部其实也是调用了 join(long timeout) 方法。
yield():Thread 类中的静态方法,当一个线程调用 yield() 方法时,实际就是在暗示线程调度器当前线程请求让出自己的 CPU ,但是线程调度器可以无条件忽略这个暗示。
sleep(long millis):线程睡眠的方法。Thread 类中的静态方法,当一个执行中的线程 A 调用了 Thread 的 sleep() 方法后,线程 A 会暂时让出指定时间的执行权,但是线程 A 所拥有的监视器资源,比如锁还是持有不让出的。指定的睡眠时间到了后该函数会正常返回,接着参与 CPU 的调度,获取到 CPU 资源后就可以继续运行。
Java 中的线程中断是一种线程间的协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是被中断的线程根据中断状态自行处理。
interrupt():中断线程,例如,当线程 A 运行时,线程 B 可以调用线程 interrupt() 方法来设置线程的中断标志为 true 并立即返回。设置标志仅仅是设置标志, 线程 A 实际并没有被中断, 会继续往下执行。
isInterrupted():检测当前线程是否被中断。
interrupted():检测当前线程是否被中断,与 isInterrupted() 不同的是,该方法如果发现当前线程被中断,则会清除中断标志。
边栏推荐
- 美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
- [input method] so far, there are so many Chinese character input methods!
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- Regular matching mailbox
- 机器学习——主成分分析(PCA)
- Nvisual digital infrastructure operation management software platform
- SSH Remote Password free login
- 线程池的执行流程
- PHP encapsulates a file upload class (supports single file and multiple file uploads)
- Canvas draw picture
猜你喜欢

JS singleton mode

线程的 sleep() 方法与 wait() 方法的区别

How to standardize data center infrastructure management process

CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance

Arbre binaire partie 1

有关二叉树 的基本操作

415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)

SQL Sever中的窗口函数row_number()rank()dense_rank()

leetCode-498: 对角线遍历

indexedDB本地存储,首页优化
随机推荐
队列Queue
Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
学习整理在php中使用KindEditor富文本编辑器
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
SQL Server AVG function rounding
numpy.logical_or
Safety and food security for teachers and students of the trapped Yingxi middle school
416 binary tree (first, middle and last order traversal iteration method)
uniapp实现点击拨打电话功能
canvas掉落的小球重力js特效动画
el-table表格的拖拽 sortablejs
利用pandas读取SQL Sever数据表
Symbol. Iterator iterator
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Cicflowmeter source code analysis and modification to meet requirements
[input method] so far, there are so many Chinese character input methods!
机器学习——感知机及K近邻
Basic operations on binary tree
413-二叉树基础