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: ... 
随机推荐
- 通过buildroot 移植 libsocketcan.so 以及 can 工具
			进入buildroot make menuconfig Target packages ---> Networking applications ---> [*] can-utils // ... 
- [script]判定某一个脚本是否正确执行
			返回内容存在$?, 0代表成功,其他代表失败 some_command ]; then echo OK else echo FAIL fi 字符型的返回值 some_command retval=$? ... 
- ThinkPHP3.2 分组分模块
			ThinkPHP/Conf/convention.php 'CONTROLLER_LEVEL' => 1, 修改成 'CONTROLLER_LEVEL' => 2, 
- A/libc:fatal signal 11(SIGSEGV).code 1, fault addr 0x0 in tid 26488 (VideoEncoder)
			在调试Camera模块:发现相同的代码在厂家提供的环境里边编译.就是ok的,在我们的源码树中编译,将HAL库推进去后.就会signal 11退出. 一.现象 F/libc ( ): Fatal sig ... 
- AWS SDK for C++调用第三方S3 API
			这里介绍AWS SDK for C++ 1.0.x版本,比如下载: https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gz 环境:RHEL/ ... 
- Python RGB 和HSV颜色相互转换
			转自:http://outofmemory.cn/code-snippet/1002/Python-RGB-HSV-color-together-switch Python RGB 和HSV颜色相互转 ... 
- Python之批量改变图片大小
			image_pylib模块:https://github.com/huangshiyu13/image_pylib data_engine模块:https://github.com/huangshiy ... 
- C++泛型和算法
			看书的速度终于慢了下来,倒不是难于理解,而是需要理解的东西有点多. 先吐槽下C++Primer这本书,不少地方都是用抽象的语言进行介绍! 拜托,你这是介绍,不是总结! 像容器适配器那里: “本质上,适 ... 
- nodejs基础 -- EventEmitter
			var events = require('events'); nodejs所有的异步I/O操作在完成时都会发送一个事件到事件队列 nodejs里面的许多对象都会分发事件,如: 一个net.Serve ... 
- 关于Random中的随机数种子Seed
			Random初始化的时候,可以以一个INT32作为参数,称为seed,MSDN上的解释是:“伪随机数是以相同的概率从一组有限的数字中选取的......随机数的生成是从种子值开始......” 所有标准 ... 
