news 2026/4/18 9:37:21

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 8:31:30

Java框架SpringBoot(一)

一、Spring的优缺点 优点 开源,轻量级,非侵入式的一站式框架,简化企业级应用开发。 控制反转(IOC),依赖注入(DI)降低了组件之间的耦合性&#xff0c;实现了软件各层之间 的解耦。 面向切面(AOP)&#xff0c;利用它可以很容易实现一些拦截&#xff0c;如事务控制等。 spring 对于…

作者头像 李华
网站建设 2026/4/16 10:51:56

JavaScript 调试

JavaScript 调试 引言 JavaScript 作为 Web 开发中的核心语言,其调试能力直接影响到开发效率和代码质量。本文将详细介绍 JavaScript 调试的基本概念、常用调试工具、调试技巧以及最佳实践,帮助开发者更好地掌握 JavaScript 调试技术。 调试的基本概念 什么是调试? 调试…

作者头像 李华
网站建设 2026/3/9 19:24:07

[数字信号处理-入门] 滤波器设计

[数字信号处理-入门] 滤波器设计 个人导航 知乎&#xff1a;https://www.zhihu.com/people/byzh_rc CSDN&#xff1a;https://blog.csdn.net/qq_54636039 注&#xff1a;本文仅对所述内容做了框架性引导&#xff0c;具体细节可查询其余相关资料or源码 参考文章&#xff1a…

作者头像 李华
网站建设 2026/4/18 8:05:18

国内旅游景点的数据爬虫与可视化分析计算机毕业设计项目源码+毕业论文+打包ppt+代码软件修改工具

博主介绍&#xff1a; CSDN毕设辅导第一人、靠谱第一人、csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客优秀创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌ 技术范围&#xff1a;…

作者头像 李华
网站建设 2026/4/18 8:15:17

大模型面试题49:从白话到进阶详解SFT 微调的 Loss 计算

SFT 的全称是 Supervised Fine-Tuning&#xff08;监督微调&#xff09;&#xff0c;它的核心目标是&#xff1a;让预训练好的大模型&#xff0c;在人工标注的「指令-回答」数据上学习&#xff0c;精准匹配人类的指令意图。 而 Loss&#xff08;损失值&#xff09;的作用&#…

作者头像 李华