java基础---->多线程之yield(三)
yield方法的作用是放弃当前的CPU资源,将它让给其它的任务去占用CPU执行时间。但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片。今天我们通过实例来学习一下yield()方法的使用。最是那一低头的温柔 像一朵水莲花不胜凉风的娇羞。
yield方法的简单实例
一、yield方法的简单使用
public class YieldThread extends Thread {
YieldThread(String string) {
super(string);
}
@Override
public void run() {
for (int i = 0; i < 10; i ++) {
System.out.println(getName() + ", " + i);
if (i % 2 == 0) {
Thread.yield();
}
}
}
}
测试的主体类如下:
public class YieldThreadTest {
public static void main(String[] args) {
YieldThread thread1 = new YieldThread("thread 1");
YieldThread thread2 = new YieldThread("thread 2");
thread1.start();
thread2.start();
}
}
一次的运行结果如下:
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
thread ,
从上述的结果可以看到每次i为偶数的时候,都会切换了线程。但是这种现象不能得到保证,只是一种趋势。yield的真正用途是:使当前线程从执行态变为可执行态,也就是就绪态吧。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 java.util.concurrent.locks package.
友情链接
java基础---->多线程之yield(三)的更多相关文章
- java基础---->多线程之Runnable(一)
java线程的创建有两种方式,这里我们通过简单的实例来学习一下.一切都明明白白,但我们仍匆匆错过,因为你相信命运,因为我怀疑生活. java中多线程的创建 一.通过继承Thread类来创建多线程 pu ...
- java基础---->多线程之synchronized(六)
这里学习一下java多线程中的关于synchronized的用法.我来不及认真地年轻,待明白过来时,只能选择认真地老去. synchronized的简单实例 一. synchronized在方法上的使 ...
- java基础---->多线程之wait和notify(八)
这里学习一下java多线程中的关于wait方法和notify方法的用法.命运不是风,来回吹,命运是大地,走到哪你都在命运中. wait和notify方法的使用 一.wait与notify的简单实例 i ...
- java基础---->多线程之ThreadLocal(七)
这里学习一下java多线程中的关于ThreadLocal的用法.人时已尽,人世还长,我在中间,应该休息. ThreadLocal的简单实例 一.ThreadLocal的简单使用 package com ...
- java基础---->多线程之interrupt(九)
这里我们通过实例来学习一下java多线程中关于interrupt方法的一些知识.执者失之.我想当一个诗人的时候,我就失去了诗,我想当一个人的时候,我就失去了我自己.在你什么也不想要的时候,一切如期而来 ...
- java基础---->多线程之Daemon(五)
在java线程中有两种线程,一种是用户线程,另一种是守护线程.守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁.今天我们通过实例来学习一下java中关于守护线程的知识.我是个平 ...
- java基础---->多线程之priority(四)
线程的priority能告诉调度程序其重要性如何,今天我们通过实例来学习一下java多线程中的关于优先级的知识.我从没被谁知道,所以也没被谁忘记.在别人的回忆中生活,并不是我的目的. java多线程的 ...
- java多线程之yield,join,wait,sleep的区别
Java多线程之yield,join,wait,sleep的区别 Java多线程中,经常会遇到yield,join,wait和sleep方法.容易混淆他们的功能及作用.自己仔细研究了下,他们主要的区别 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
随机推荐
- java依赖的外部文件路径的获取
在开发阶段一直使用以下方式调试没有问题: String path = KStream104.class.getResource("/").getFile().toString(); ...
- implicit declaration of function 'copy_from_user'
内核中使用copy_from_user()和copy_to_user()函数,编译出现错误: implicit declaration of function 'copy_from_user' 需要添 ...
- linux -- chown修改文件拥有者和所在组
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- java动态代码的实现以及Class的卸载 (转至http://dustin.iteye.com/blog/46393)
JavaWorld一篇题为 Add dynamic code to your application 的文章介绍了如何使用动态代理技术使普通的java源代码具有像jsp一样的动态编译效果,十分有趣. ...
- ST500LT012-1DG142硬盘參数
ATA 设备物理信息 制造商 Seagate 硬盘名称 Momentus Thin 500LT012 形状特征 2.5" 格式化容量 500 GB 盘片数 1 记录面 2 外形尺寸 100 ...
- struts2零配置參考演示样例
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...
- 【MYSQL】导入中文后乱码问题
http://fatkun.com/2011/05/mysql-alter-charset.html
- Ubuntu 12.04.2 安装 Oracle11gR2
#step 1: groupadd -g 2000 dbauseradd -g 2000 -m -s /bin/bash -u 2000 griduseradd -g 2000 -m -s /bin/ ...
- SVN入门 TortoiseSVN 检出
1. SVN检出(SVN Checkout) 检出项目文件. 新建或者进入目录下(比如qianduan1),右键 --> Svn 检出-->其中版本库URL我可以在SVN服务器获取到,将复 ...
- 通过xtrabackup工具对mysql数据库做全备
需求描述: 使用xtrabackup工具对mysql实例中的所有的数据库进行备份,并且将备份的过程输出到指定的日志文件中. 操作过程: 1.使用xtrabacup工具备份mysql实例 xtrabac ...