这是Java中的一种线程让步方法,让Java中的线程从执行状态变成就绪状态,然后处理器再从就绪队列中挑选线程进行执行(优先级大的,被挑选的概率较大),这种转换也不确定,让或者不让都是取决与处理器,线程可能继续占有处理器。

**
* 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();

  看《Java并发编程艺术》这本书,里面有用到Thread.yield方法,来测试Java线程优先级,代码如下:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; public class Priority {
private static volatile boolean notStart=true;
private static volatile boolean notEnd=true; public static void main(String[] argvs) throws Exception{
List<Job> jobs=new ArrayList<Job>();
for(int i=0;i<10;i++){
int priority=i<5?Thread.MIN_PRIORITY:Thread.MAX_PRIORITY;
Job job=new Job(priority);
jobs.add(job);
Thread thread=new Thread(job,"Thread:"+i);
thread.setPriority(priority);
thread.start();
}
notStart=false;
TimeUnit.SECONDS.sleep(10);
notEnd=false; for(Job job:jobs){
System.out.println("Job Priority:"+job.priority+", Count: "+job.jobCount);
}
} static class Job implements Runnable{
private int priority;
private long jobCount; public Job(int priority){
this.priority=priority;
} @Override
public void run() {
while(notStart){
Thread.yield();
}
while(notEnd){
Thread.yield();
jobCount++;
}
}
}
}

  跑出结果如下:

Job Priority:1, Count: 6087836
Job Priority:1, Count: 6573345
Job Priority:1, Count: 6024024
Job Priority:1, Count: 6606573
Job Priority:1, Count: 6901572
Job Priority:10, Count: 6118724
Job Priority:10, Count: 5968747
Job Priority:10, Count: 5939391
Job Priority:10, Count: 6206129
Job Priority:10, Count: 6187854

 发现即使线程优先级为1和为10的线程,最后获取CPU的次数是差不多的,没有明显差距,线程优先级没有生效。程序的正确性不能依赖于线程的优先级。

Java Thread.yield详解的更多相关文章

  1. 【转】Java Thread.join()详解

    http://www.open-open.com/lib/view/open1371741636171.html 一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: ? 1 ...

  2. Java Thread.join()详解(转)

    (1)join方法是可以中断的(2)在线程joiner在另一个线程t上调用t.join(),线程joiner将被挂起,直到线程t结束(即t.isAlive()返回为false)才恢复 package ...

  3. Java Thread.join()详解--父线程等待子线程结束后再结束

    目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   join是Th ...

  4. Java Thread.join()详解

    一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   一.使用方式. join是Thread类的 ...

  5. Java基础-进程与线程之Thread类详解

    Java基础-进程与线程之Thread类详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程与线程的区别 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 ...

  6. java.lang.Thread类详解

    java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...

  7. Java线程创建形式 Thread构造详解 多线程中篇(五)

    Thread作为线程的抽象,Thread的实例用于描述线程,对线程的操纵,就是对Thread实例对象的管理与控制. 创建一个线程这个问题,也就转换为如何构造一个正确的Thread对象. 构造方法列表 ...

  8. thread 类详解

    java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...

  9. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

随机推荐

  1. [ErrorException] "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?

    Mac上PHP更新到7.3,使用Composer报这个错误 解决办法: composer selfupdate

  2. JAVA比较两个List集合的方法

    import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.H ...

  3. Java Selenium - 元素操作 (三)

    接上一篇,我们依然以京东的网站做示例. 三,单选项 下面来做这样一条case: 1. 登录京东旅行网页. 2. 在国内机票板块,购买从北京到武汉的往返机票,时间为明天出发,一周后返回. 3.搜索机票. ...

  4. 使用VS2013自带的PreEmptive Dotfuscator and Analytis来混淆C#代码

    1. 使用VS2013编译你要打包的程序,会在文件夹中的 ..\bin\Release中 2. 点击VS2013中的TOOLS -> PreEmptive Dotfuscator and Ana ...

  5. Nodejs基础(5-6)HTTP概念进阶

    1.什么是回调? 是异步编程最基本的方法,对于nodejs来说需要按顺序执行异步逻辑的时候一般采用后续传递的方式,也就是将后续逻辑封装在回调函数中作为起始函数的参数逐层去嵌套.通过这种方式来让程序按照 ...

  6. Linux 配置yum源.

    Linux 配置yum源. 环境:虚拟机中安装了RedHat ,在进行安装mariadb的时候,出现如下错误.是因为yum源的问题,需要进行配置yum源.本教程是配置本地yum源. [root@loc ...

  7. DataGridView控件用法一:数据绑定

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  8. eclipse中tomcat启动成功,浏览器访问失败

    eclipse添加tomcat之后,tomcat有个默认设置,我们需要对tomcat进行重新设置: 1.双击已添加的tomcat,进入到配置页面,找到server locations一栏,可以看到默认 ...

  9. Marlin 溫度 sensor 校正

    Marlin 溫度 sensor 校正 使用 Type-K 溫度計 將探針綑綁在加熱頭側面 開啟Marlin-Marlin_v1\Marlin\thermistortables.h 要修改的溫度對應表 ...

  10. 排序(Sort)-----插入排序

       声明:文中动画转载自https://blog.csdn.net/qq_34374664/article/details/79545940    1.插入排序简介 插入排序(InsertSort) ...