news 2026/4/16 14:25:28

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/16 11:00:16

通信协议仿真:蓝牙协议仿真_(4).蓝牙低功耗协议

蓝牙低功耗协议 引言 蓝牙低功耗&#xff08;Bluetooth Low Energy&#xff0c;简称BLE&#xff09;是一种旨在降低功耗的无线通信技术&#xff0c;广泛应用于健康、运动、智能家居等领域。与经典蓝牙相比&#xff0c;BLE在功耗、连接速度和数据传输方面有显著的改进。本节将详…

作者头像 李华
网站建设 2026/4/16 12:52:03

jEasyUI 设置冻结列详解

jEasyUI 设置冻结列详解 引言 jEasyUI 是一款流行的前端框架,它简化了网页的UI开发。在jEasyUI中,表格是常用的组件之一。冻结列功能允许用户在滚动表格时保持某些列固定显示,这对于展示大量数据时保持关键信息的可见性非常有用。本文将详细讲解如何在jEasyUI中设置冻结列…

作者头像 李华
网站建设 2026/4/12 19:23:55

XQuery 语法概述

XQuery 语法概述 XQuery 是一种用于查询结构化数据的查询语言,它主要应用于 XML 和 XPath 数据源。本篇文章将对 XQuery 的基本语法进行详细介绍,包括数据模型、数据类型、表达式、查询结构以及常用的操作符。 数据模型 XQuery 的数据模型主要包括以下几种类型: XML 文档…

作者头像 李华
网站建设 2026/4/16 12:44:53

XHTML 简介

XHTML 简介 引言 随着互联网技术的飞速发展,网页设计已经成为一个重要的领域。HTML(超文本标记语言)作为网页设计的基础,一直被广泛使用。然而,随着技术的发展,HTML面临着一些新的挑战。为了解决这些问题,XHTML(可扩展超文本标记语言)应运而生。本文将详细介绍XHTML…

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

我想知道相机的不同系列,比如因特尔realsense、奥比中光的不同系列相机,他们的SDK一样吗?或者说一般一样嘛?

我想知道相机的不同系列&#xff0c;比如因特尔realsense、奥比中光的不同系列相机&#xff0c;他们的SDK一样吗&#xff1f;或者说一般一样嘛&#xff1f;了解了&#xff01;你是指同一品牌下不同系列相机的SDK是否相同或类似&#xff1f;对于 Intel RealSense 和 奥比中光&am…

作者头像 李华
网站建设 2026/4/16 11:00:20

[数字信号处理-入门] 采样定理

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

作者头像 李华