news 2026/4/15 8:54:11

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/14 9:29:45

通信协议仿真:蓝牙协议仿真_(6).蓝牙网络拓扑

蓝牙网络拓扑 1. 蓝牙网络基础 蓝牙网络拓扑是指蓝牙设备在无线通信网络中的组织和连接方式。理解蓝牙网络的基本概念和拓扑结构对于进行蓝牙协议仿真至关重要。蓝牙网络可以分为两种主要类型&#xff1a;基础模式&#xff08;Basic Rate/Enhanced Data Rate, BR/EDR&#xff0…

作者头像 李华
网站建设 2026/4/14 0:23:41

通信协议仿真:蓝牙协议仿真_(9).蓝牙仿真案例分析

蓝牙仿真案例分析 在上一节中&#xff0c;我们已经介绍了蓝牙协议的基本概念和结构。本节将通过具体的案例来分析蓝牙协议的仿真过程&#xff0c;帮助读者更好地理解如何在实际开发中应用蓝牙仿真技术。我们将从以下几个方面进行详细的分析&#xff1a; 蓝牙设备连接仿真蓝牙数…

作者头像 李华
网站建设 2026/4/14 11:11:04

Java实战:Spring Boot application.yml配置文件详解

本文将详细介绍Spring Boot application.yml 配置文件的使用和配置项。我们将探讨 application.yml 文件的基本概念&#xff0c;以及如何使用它来配置Spring Boot应用程序的各个方面。此外&#xff0c;我们将通过具体的示例来展示如何配置不同的Spring Boot组件&#xff0c;如数…

作者头像 李华
网站建设 2026/4/15 1:11:51

51单片机__LED相关

51单片机__LED相关 单片机介绍 单片机&#xff0c;英文Micro Controller Unit&#xff0c;简称MCU 内部集成了CPU、RAM、ROM、定时器、中断系统、通讯接口等一系列电脑的常用硬件功能 单片机的任务是信息采集&#xff08;依靠传感器&#xff09;、处理&#xff08;依靠CPU&…

作者头像 李华
网站建设 2026/4/3 23:22:15

Prompt Tuning动态选医疗特征提速诊断

&#x1f4dd; 博客主页&#xff1a;Jax的CSDN主页 Prompt Tuning动态选医疗特征提速诊断 目录Prompt Tuning动态选医疗特征提速诊断 引言&#xff1a;诊断效率的全球性挑战 技术原理&#xff1a;动态特征选择的机制创新 现实应用&#xff1a;2023年临床试点的突破性验证 挑战与…

作者头像 李华