Thread#yield方法表示“暂停当前正在执行的线程对象,并执行其他线程”。在《The Java Language Specification, Third
Edition》的17.9 Sleep and Yield 一节中是这样描述的:
Thread.sleep causes the currently executing thread to sleep (temporarily
cease execution) for the specified duration, subject to the precision and
accuracy of system timers and schedulers. The thread does not lose ownership
of any monitors, and resumption of execution will depend on scheduling and
the availability of processors on which to execute the thread.
Neither a sleep for a period of zero time nor a yield operation need
have observable effects.
It is important to note that neither Thread.sleep nor Thread.yield have
any synchronization semantics. In particular, the compiler does not have to
flush writes cached in registers out to shared memory before a call to
Thread.sleep or Thread.yield, nor does the compiler have to reload values
cached in registers after a call to Thread.sleep or Thread.yield.
|
而在《The Java Language Specification, Java SE 7
Edition》17.3. Sleep and Yield 一节中是这样描述的:
Thread.sleep causes the currently executing thread to sleep
(temporarily cease execution) for the specified duration, subject to the
precision and accuracy of system timers and schedulers. The thread does not
lose ownership of any monitors, and resumption of execution will depend on
scheduling and the availability of processors on which to execute the thread.
It is important
to note that neither Thread.sleep nor Thread.yield have
any synchronization semantics. In particular, the compiler does not have to
flush writes cached in registers out to shared memory before a call
to Thread.sleep or Thread.yield, nor does the compiler have to
reload values cached in registers after a call
to Thread.sleep or Thread.yield.
|
对比新版的JLS与旧版的JLS,少了句“Neither a sleep for a period of zero time nor a yield
operation need have observable effects”
这里有一些“说明”:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6584700
Section 17.9 "Sleep and Yield" in the Java Language
Specification Third Edition, has the following statement:
"Neither a sleep for a
period of zero time nor a yield operation need have observable effects."
This precludes java.lang.Thread.yield from having any strong
specification as it could be challenged as a contradiction to this statement.
Effectively this statement in the JLS allows yield to be a noop and prevents
it from gaining any strongly specified behavior.
|
请教了下RednaxelaFX,他这么说的:“范里去掉这句话也没保证sleep(0)和yield()一定有效果,只是懒得被人挑刺而已吧”。
JDK1.6之前的javaDoc中都是这么写的:
public static void yield()
Causes the
currently executing thread object to temporarily pause and allow other
threads to execute.
|
在新版的JavaDoc中,做了修正:
public static void yield()
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 java.util.concurrent.locks package.
|
描述的更清晰易懂了。
同时,RednaxelaFX 还提到:“HotSpot VM的当前版本(JDK6、JDK7)的Linux版里的Thread.yield()实现默认跑到最底下是sched_yield(),http://www.kernel.org/doc/man-pages/online/pages/man2/sched_yield.2.html;同版本里Thread.sleep(0)会转换为跟Thread.yield()等价的行为,也是调用到sched_yield()”。
分享到:
相关推荐
通过以下示例代码,我们可以更直观地了解`sleep`和`yield`方法的效果: ```java public class SleepYieldExample { public static void main(String[] args) { Thread xiaoming = new Thread(() -> { for (int i...
3. **SYSTEM文件夹的修改**:这部分可能涉及RT-Thread的系统配置,包括内存分配、时钟管理、中断服务等,学习者会了解如何根据实际需求调整RT-Thread的系统设置。 4. **邮箱**:邮箱是RT-Thread中一种数据通信机制...
该压缩包包含了多个实验项目,旨在帮助学习者深入理解和掌握RT-Thread在STM32F103微控制器上的应用。 首先,RT-Thread是一个开源、轻量级、高可扩展性的实时操作系统,广泛应用于物联网设备和嵌入式系统中。它提供...
### 操作系统Pintos中的进程优先级调度实现 #### 背景介绍 Pintos是一个用于教学目的的操作系统框架,它可以帮助学生...通过上述详细步骤,读者可以深入了解如何在Pintos或其他类似的简单操作系统中实现这一重要功能。
首先,我们要了解Java中实现多线程的两种主要方式:继承Thread类和实现Runnable接口。当创建一个新线程时,通常会覆盖`Thread.run()`方法或实现`Runnable.run()`方法。在"thread-multi.zip"中,我们可能会看到这两种...
本资料“Java Thread Programming”由Paul Hyde提供,包含了关于Java线程编程的理论知识和实践代码,旨在帮助开发者深入理解和熟练掌握Java线程。 首先,我们来了解一下Java中线程的基本概念。在Java中,可以通过两...
开发者需要了解并发控制原理,如使用`synchronized`、`volatile`、`ReentrantLock`等机制,以及避免使用静态变量作为共享数据,以确保程序的正确性和稳定性。 总结,`Thread`类在Java并发编程中扮演着核心角色,...
通过研究这个"javathread"素材包,你可以深入了解Java线程的创建、控制、同步和通信,提升你在并发编程中的能力。实践是检验真理的唯一标准,尝试运行这些示例,观察其运行结果,分析其背后的工作原理,这将对你的...
#### 五、`Thread.sleep()`方法深入解析 `Thread.sleep()`是`Thread`类的一个静态方法,用于使当前正在执行的线程暂停指定的毫秒数。使用此方法时,必须处理`InterruptedException`,因为线程可能会被中断,从而抛...
- `Thread.yield()`:使当前线程放弃剩余时间片,重新回到就绪状态。 - `Thread.interrupt()`:中断一个线程。 - `Thread.isInterrupted()`:判断当前线程是否被中断。 #### 五、线程同步与互斥 在多线程环境下,...
通过以上三个实验,我们不仅学习了如何创建和管理Java线程,还深入了解了线程的生命周期以及线程同步的基本原理。这些知识对于开发高性能、高并发的应用程序至关重要。希望你能通过实践加深对这些概念的理解,并将其...
如果`err`被设置为`1`,则会在删除操作之前执行`currentThread->Yield();`,这会导致当前线程放弃CPU资源,使得其他线程有机会执行。在某些情况下,这种非确定性的线程切换可能会导致`first`指针指向非法地址,最终...
《lua5.2中文参考手册》作为一份详尽的技术文档,为用户提供了一个全面了解 Lua 5.2 的窗口。本文将根据提供的信息摘要出其中的关键知识点,并进行深入解读。 #### 2. 基本概念 ##### 2.1 值与类型 - **基本类型*...
### Java多线程详解 #### 一、Java多线程概览 ...开发者应当深入了解Java多线程的基本概念、创建方式以及常用的线程控制方法,并结合实际应用场景灵活运用高级特性,以提升系统的并发处理能力和稳定性。
通过上述知识点的详细阐述,我们可以看到Java线程学习不仅仅是对线程基本概念的掌握,更是对线程控制、状态管理以及同步机制的深入了解和实践应用。这对于开发高效、健壮的多线程应用程序至关重要。
在实际项目中,我们还可以结合使用`boost::this_thread::yield()`来改善线程调度,使用`boost::thread_group`管理一组线程,使用`boost::asio`库实现异步I/O操作等。 总结来说,Boost线程库提供了强大的多线程编程...
4. 线程礼让:通过Thread.yield()方法让当前线程暂停,但不会释放CPU,而是让操作系统决定是否切换到其他线程。 5. 线程休眠:通过Thread.sleep(long milliseconds)使线程进入阻塞状态,指定时间后自动恢复。 6. ...
下面通过几个例子来深入了解这一关键字的应用场景: 1. **示例2**: ```java public class ThreadTest implements Runnable { public synchronized void run() { for (int i = 0; i ; i++) { System.out.print...