java sleep函数让出cpu_Java 进阶:如何让线程主动让出 CPU

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. * *

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. * *

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. *

* First, the checkAccess method of this thread is called * with no arguments. This may result in throwing a * SecurityException (in the current thread). *

* 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 resume, deadlock results. Such * deadlocks typically manifest themselves as "frozen" processes. * For more information, see * Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?. */

@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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值