当前位置:网站首页>线程调度的常用方法
线程调度的常用方法
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() 不同的是,该方法如果发现当前线程被中断,则会清除中断标志。
边栏推荐
- Development of anti fleeing marketing software for health products
- Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
- leetCode-1051: 高度检查器
- GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
- Regular matching mobile number
- Cicflowmeter source code analysis and modification to meet requirements
- NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
- Record the range of data that MySQL update will lock
- SSH Remote Password free login
- SQL sever试题求最晚入职日期
猜你喜欢
随机推荐
414-二叉树的递归遍历
Which of the top ten securities companies has the lowest Commission and is the safest and most reliable? Do you know anything
简单的价格表样式代码
p5.js千纸鹤动画背景js特效
小程序 rich-text中图片点击放大与自适应大小问题
Indexeddb local storage, homepage optimization
416 binary tree (first, middle and last order traversal iteration method)
Troubleshooting steps for Oracle pool connection request timeout
读取csv(tsv)文件出错
Three ways to use applicationcontextinitializer
el-table表格的拖拽 sortablejs
What are the characteristics of EDI local deployment and cloud hosting solutions?
线程池的状态
1.项目环境搭建
一群骷髅在飞canvas动画js特效
Machine learning perceptron and k-nearest neighbor
机器学习——主成分分析(PCA)
Binary tree part I
微信小程序學習之 實現列錶渲染和條件渲染.
学习使用php对字符串中的特殊符号进行过滤的方法









