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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Nacos集群搭建过程详解

    Nacos集群搭建过程详解

    这篇文章主要为大家介绍了Nacos集群搭建过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Java 时间日期详细介绍及实例

    Java 时间日期详细介绍及实例

    这篇文章主要介绍了Java 时间日期详细介绍及实例的相关资料,需要的朋友可以参考下
    2017-01-01
  • 解决Spring Security中AuthenticationEntryPoint不生效相关问题

    解决Spring Security中AuthenticationEntryPoint不生效相关问题

    这篇文章主要介绍了解决Spring Security中AuthenticationEntryPoint不生效相关问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • idea设置@Author文件头注释的实现步骤

    idea设置@Author文件头注释的实现步骤

    本文主要介绍了idea设置@Author文件头注释的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • java list与数组之间的转换详细解析

    java list与数组之间的转换详细解析

    以下是对java中list与数组之间的转换进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-09-09
  • Spring Event事件通知机制解读

    Spring Event事件通知机制解读

    这篇文章主要介绍了Spring Event事件通知机制解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Mybatis调用Oracle存储过程的方法图文详解

    Mybatis调用Oracle存储过程的方法图文详解

    这篇文章主要介绍了Mybatis调用Oracle存储过程的方法介绍,需要的朋友可以参考下
    2017-09-09
  • Java8 接口默认方法和静态方法

    Java8 接口默认方法和静态方法

    这篇文章主要介绍了Java8 接口默认方法和静态方法,在默认接口中使用关键字default声明并提供具体实现,而且该方法不需要添加public关键字就可以公开调用,甚至你可以在其实现类中覆写,带着对默认接口的方法和小编一起探索下面文章内容的静态方法吧
    2021-10-10
  • SpringBoot+MinIO实现对象存储的示例详解

    SpringBoot+MinIO实现对象存储的示例详解

    MinIO 是一个基于Apache License v2.0开源协议的对象存储服务,它是一个非常轻量的服务,可以很简单的和其他应用的结合,所以下面我们就来看看SpringBoot如何整合MinIO实现对象存储吧
    2023-10-10
  • servlet3新特性_动力节点Java学院整理

    servlet3新特性_动力节点Java学院整理

    这篇文章主要为大家详细介绍了servlet3新特性的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论