Java如何让线程主动让出 CPU

 更新时间:2026年05月12日 08:37:16   作者:ybwycx  
本文主要介绍了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" rel="external nofollow" >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

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

到此这篇关于Java如何让线程主动让出 CPU的文章就介绍到这了,更多相关Java 主动让出 CPU内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java线程池工作队列饱和策略代码示例

    java线程池工作队列饱和策略代码示例

    这篇文章主要介绍了java线程池工作队列饱和策略代码示例,涉及线程池的简单介绍,工作队列饱和策略的分析及代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Java 详细讲解线程的状态及部分常用方法

    Java 详细讲解线程的状态及部分常用方法

    在Java程序中,一个线程对象只能调用一次start()方法启动新线程,并在新线程中执行run()方法。一旦run()方法执行完毕,线程就结束了,本篇来讲解Java线程的状态以及部分常用方法
    2022-04-04
  • Spring IOC简单理解及创建对象的方式

    Spring IOC简单理解及创建对象的方式

    这篇文章主要介绍了Spring IOC简单理解及创建对象的方式,本文通过两种方式给大家介绍创建对象的方法,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • Java 超详细讲解异常的处理

    Java 超详细讲解异常的处理

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等
    2022-04-04
  • 设置Myeclipse中的代码格式化、注释模板及保存时自动格式化

    设置Myeclipse中的代码格式化、注释模板及保存时自动格式化

    这篇文章主要介绍了设置Myeclipse中的代码格式化、注释模板及保存时自动格式化方法,需要的朋友可以参考下
    2014-10-10
  • Java使用Freemarker页面静态化生成的实现

    Java使用Freemarker页面静态化生成的实现

    这篇文章主要介绍了Java使用Freemarker页面静态化生成的实现,页面静态化是将原来的动态网页改为通过静态化技术生成的静态网页,FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输,更多相关内容需要的小伙伴可以参考一下
    2022-06-06
  • Java实现视频格式转化的操作代码

    Java实现视频格式转化的操作代码

    在当今数字化时代,视频已成为我们日常生活和工作中不可或缺的一部分,不同的设备和平台可能支持不同的视频格式,因此,视频格式转换的需求也日益增长,本文将介绍如何使用Java实现视频格式转换,需要的朋友可以参考下
    2025-01-01
  • Java 包和访问权限操作

    Java 包和访问权限操作

    这篇文章主要介绍了Java 包和访问权限操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • springboot实现jar运行复制resources文件到指定的目录(思路详解)

    springboot实现jar运行复制resources文件到指定的目录(思路详解)

    这篇文章主要介绍了springboot实现jar运行复制resources文件到指定的目录,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • IDEA下因Lombok插件产生的Library source does not match the bytecode报错问题及解决方法(亲测可用)

    IDEA下因Lombok插件产生的Library source does not match the bytecode报

    这篇文章主要介绍了IDEA下因Lombok插件产生的Library source does not match the bytecode报错问题及解决方法,亲测试过好用,需要的朋友可以参考下
    2020-04-04

最新评论