资讯专栏INFORMATION COLUMN

java Thread yield()方法的理解

frank_fun / 1894人阅读

摘要:先看一下源码是一个方法也就是说是或者实现的概念当调用的时候,会给线程调度器一个当前线程愿意出让的使用的暗示,但是线程调度器可能会忽略这个暗示。

先看一下源码 yield()是一个native方法也就是说是C或者C++实现的

  /**
     * 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.yield()的时候,会给线程调度器一个当前线程愿意出让CPU的使用的暗示,但是线程调度器可能会忽略这个暗示。

代码

public class Demo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("当前线程为: "+ Thread.currentThread().getName()+ i);
                    if (i == 5){
                        Thread.yield();
                    }
                }
            }
        };

        Thread thread = new Thread(runnable,"A");
        Thread thread1 = new Thread(runnable,"B");
        thread.start();
        thread1.start();
    }
}

第一次执行结果

当前线程为: A0
当前线程为: B0
当前线程为: A1
当前线程为: B1
当前线程为: A2
当前线程为: B2
当前线程为: A3
当前线程为: B3
当前线程为: A4
当前线程为: B4
当前线程为: A5
当前线程为: B5
当前线程为: A6
当前线程为: B6
当前线程为: A7
当前线程为: B7
当前线程为: B8
当前线程为: A8
当前线程为: A9
当前线程为: B9
第二次执行结果

当前线程为: B0
当前线程为: B1
当前线程为: B2
当前线程为: B3
当前线程为: B4
当前线程为: B5
当前线程为: B6
当前线程为: B7
当前线程为: B8
当前线程为: B9
当前线程为: A0
当前线程为: A1
当前线程为: A2
当前线程为: A3
当前线程为: A4
当前线程为: A5
当前线程为: A6
当前线程为: A7
当前线程为: A8
当前线程为: A9
第三次执行结果

当前线程为: A0
当前线程为: A1
当前线程为: A2
当前线程为: A3
当前线程为: A4
当前线程为: A5
当前线程为: A6
当前线程为: A7
当前线程为: A8
当前线程为: A9
当前线程为: B0
当前线程为: B1
当前线程为: B2
当前线程为: B3
当前线程为: B4
当前线程为: B5
当前线程为: B6
当前线程为: B7
当前线程为: B8
当前线程为: B9
第四次执行结果

当前线程为: A0
当前线程为: A1
当前线程为: A2
当前线程为: A3
当前线程为: A4
当前线程为: A5
当前线程为: A6
当前线程为: A7
当前线程为: A8
当前线程为: A9
当前线程为: B0
当前线程为: B1
当前线程为: B2
当前线程为: B3
当前线程为: B4
当前线程为: B5
当前线程为: B6
当前线程为: B7
当前线程为: B8
当前线程为: B9
第五次执行结果

当前线程为: A0
当前线程为: B0
当前线程为: A1
当前线程为: B1
当前线程为: A2
当前线程为: B2
当前线程为: A3
当前线程为: B3
当前线程为: A4
当前线程为: B4
当前线程为: A5
当前线程为: B5
当前线程为: A6
当前线程为: B6
当前线程为: A7
当前线程为: B7
当前线程为: B8
当前线程为: A8
当前线程为: A9
当前线程为: B9
说明每次执行的可能都不一样,但是当执行到i == 5时候当前线程可能继续执行也可能让B执行。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75063.html

相关文章

  • javathread.yield

    摘要:,看到很多文章把理解为暂停,其实是不准确的,翻译过来用让步更为准确一些。如下在中我们可以看到官方的解释,更多的是作为调试或测试时候使用也就是疯狂重分配,提高切换概率,模拟并发 yield,看到很多文章把yield理解为暂停,其实是不准确的,翻译过来用让步更为准确一些。简单描述下其作用: 使调用yield的正在执行的线程让出cpu,让同等优先权的其他线程包括自身重新进行分配调度 概念性的...

    YPHP 评论0 收藏0
  • Java并发编程——线程基础查漏补缺

    摘要:告诉当前执行的线程为线程池中其他具有相同优先级的线程提供机会。不能保证会立即使当前正在执行的线程处于可运行状态。当达到超时时间时,主线程和是同样可能的执行者候选。下一篇并发编程线程安全性深层原因 Thread 使用Java的同学对Thread应该不陌生了,线程的创建和启动等这里就不讲了,这篇主要讲几个容易被忽视的方法以及线程状态迁移。 wait/notify/notifyAll 首先我...

    luqiuwen 评论0 收藏0
  • @Java | Thread & synchronized - [ 多线程 基本使用]

    摘要:线程线程是进程中的一个实体,作为系统调度和分派的基本单位。下的线程看作轻量级进程。因此,使用的目的是让相同优先级的线程之间能适当的轮转执行。需要注意的是,是线程自己从内部抛出的,并不是方法抛出的。 本文及后续相关文章梳理一下关于多线程和同步锁的知识,平时只是应用层面的了解,由于最近面试总是问一些原理性的知识,虽说比较反感这种理论派,但是为了生计也必须掌握一番。(PS:并不是说掌握原理不...

    zhunjiee 评论0 收藏0
  • Thread类源码解读(2)——线程状态及常用方法

    摘要:如果线程还存活,线程就无限期等待,并让出监视器锁,进入状态。当线程从状态被唤醒后通过,或者是假唤醒将继续竞争监视器锁,当成功获得监视器锁后,他将从调用的地方恢复,继续运行。 前言 系列文章目录 上一篇我们讨论了线程的创建,本篇我们来聊一聊线程的状态转换以及常用的几个比较重要的方法。 本篇依然是通过源码分析来了解这些知识。 本文源码基于jdk1.8 。 阅读完本文,你应当有能力回答以...

    luqiuwen 评论0 收藏0
  • Java多线程笔记(一):JMM与基础关键字

    摘要:当线程执行完后进入状态,表示线程执行结束。其中和表示两个线程。但要注意,让出并不表示当前线程不执行了。关键字其作用是防止指令重排和使线程对一个对象的修改令其他线程可见。 JMM特性一览 Java Memory Model的关键技术点都是围绕着多线程的原子性、可见性和有序性来建立的。因此我们首先需要来了解这些概念。 原子性(Atomicity) 原子性是指一个操作是不可中断的。即使是在多...

    cyixlq 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<