当前位置: 首页 > news >正文

Java 进阶:如何让线程主动让出 CPU

  • Java 进阶如何让线程主动让出 CPU
    • Threadsleep
    • Threadyield
    • ThreadcurrentThreadsuspend
    • Objectwait
    • LockSupportpark
    • Threadstop

Java 进阶:如何让线程主动让出 CPU

Thread.sleep

sleep 方法可以让线程主动让出 CPU,但是并不会释放锁。

/** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. */ public static native void sleep(long millis) throws InterruptedException;

Thread.yield

yield 也可以让线程主动让出 CPU,然后和其他线程一起竞争 CPU,但是调度器也可以忽略 yield。哪些情况会用到 yield 呢?
1. 一般在 debug 和 test 中使用。
2. CPU 密集型应用主动让出 CPU 以避免过度占用 CPU,影响其他任务。

/** * A hint to the scheduler that the current thread is willing to yield * its current use of a processor. The scheduler is free to ignore this * hint. * * <p> Yield is a heuristic attempt to improve relative progression * between threads that would otherwise over-utilise a CPU. Its use * should be combined with detailed profiling and benchmarking to * ensure that it actually has the desired effect. * * <p> It is rarely appropriate to use this method. It may be useful * for debugging or testing purposes, where it may help to reproduce * bugs due to race conditions. It may also be useful when designing * concurrency control constructs such as the ones in the * {@link java.util.concurrent.locks} package. */ public static native void yield();

Thread.currentThread().suspend()

该方法已过时。为啥呢?suspend 挂起线程,并不会释放锁,又不像 sleep 那样一段时间后自动恢复,所以容易引起死锁。相对应的 resume 方法用于唤醒一个 suspend 的线程。

/** * Suspends this thread. * <p> * First, the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException </code>(in the current thread). * <p> * If the thread is alive, it is suspended and makes no further * progress unless and until it is resumed. * * @exception SecurityException if the current thread cannot modify * this thread. * @see #checkAccess * @deprecated This method has been deprecated, as it is * inherently deadlock-prone. If the target thread holds a lock on the * monitor protecting a critical system resource when it is suspended, no * thread can access this resource until the target thread is resumed. If * the thread that would resume the target thread attempts to lock this * monitor prior to calling <code>resume</code>, deadlock results. Such * deadlocks typically manifest themselves as "frozen" processes. * For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void suspend() { checkAccess(); suspend0(); } @Deprecated public final void resume() { checkAccess(); resume0(); }

Object.wait

wait 会把当前持有的锁释放掉同时阻塞住,让出 CPU。当其他线程调用 Object.notify/notifyAll 时,会被唤醒,可能得到 CPU,并且获得锁。

LockSupport.park

这就是锁了嘛,相对应的用 unpark 解锁。

Thread.stop

该方法已过时,直接停止线程,同时会释放所有锁,太过暴力,容易导致数据不一致。

http://www.jsqmd.com/news/212015/

相关文章:

  • 亲测好用10个AI论文工具,专科生轻松搞定论文写作!
  • JavaScript 调试
  • AngularJS 模块详解
  • 救命神器!自考必看TOP9 AI论文网站测评与推荐
  • idea创建springBoot的五种方式
  • 毫米波V2I网络的链路层仿真研究(Matlab代码实现)
  • Java实战:Spring Boot application.yml配置文件详解
  • python_flask求职招聘岗位信息分析系统的设计与实现_xz0yin70可视化大屏
  • Prompt Tuning动态选医疗特征提速诊断
  • idea、mybatis报错Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
  • Azure GPv1 存储账户迁移至 GPv2 完整指南
  • Java学习进阶知识篇
  • Memcached 连接:深入理解与优化实践
  • 深度学习计算机毕设之基于python训练鲜花识别基于卷神经网络训练鲜花识别
  • HeidiSQL导入与导出数据
  • 2026最全Gemini学生优惠申请指南
  • AS7341可见光谱传感器原理图设计,已量产(光线传感器)
  • 文档解析API哪个好用
  • 俄罗斯总统透露:美国想在扎波罗热核电站进行加密货币挖矿?
  • 三一重工标杆企业游学—走进名企,对标精益生产管理
  • IDEA + Spring Boot 的三种热加载方案,看完弄懂,不用加班
  • 2026Teng讯总部游学:解码数字经济,锚定营销增长新坐标
  • 深度学习毕设选题推荐:基于人工智能的玉米粒品质检测基于深度学习的玉米粒品质检测
  • 环境振动估算阻尼比 (SDOF)研究(Matlab代码实现)
  • 【毕业设计】基于python训练鲜花识别基于python机器学习训练鲜花识别
  • 彼得林奇的“成长型价值股“定义
  • 2026无锡研学机构TOP10精简版|3分钟选对不踩坑
  • IDEA+Docker一键部署项目SpringBoot项目
  • 2026年乌镇黑科技,AI眼镜解放双手,逛展不用愁!
  • golang debug调试