yield java示例_Java线程类静态本机void yield()方法(带示例)

yield java示例

线程类静态本机void yield() (Thread Class static native void yield())

  • This method is available in package java.lang.Thread.yield().

    软件包java.lang.Thread.yield()中提供了此方法。

  • yield() method says to stop the currently executing thread and will give a chance to other waiting threads of the same priority.

    yield()方法表示停止当前正在执行的线程,并为其他优先级相同的等待线程提供机会。

  • If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution.

    如果没有等待线程,或者所有等待线程的优先级都较低,则同一线程将继续执行。

  • The advantage of this method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads.

    这种方法的优点是有机会执行其他等待线程,因此,如果我们当前的线程需要更多时间来执行并将处理器分配给其他线程。

  • This method is static so we can access this method with the class name too.

    该方法是静态的,因此我们也可以使用类名访问此方法。

  • This is a native method that means the implementation of this method is available in other languages like C, C++ so whenever we need this method so we can declare in our class.

    这是一个本地方法,这意味着该方法的实现在其他语言(例如C,C ++)中可用,因此只要需要此方法,就可以在类中声明。

  • We can't expect when yielded will get a chance it is decided by the Thread Scheduler.

    我们不能期望何时产生收益将由线程调度程序决定。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

Syntax:

句法:

    static native void yield(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the Thread.

我们不会在Thread方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序演示yield()方法的示例 (Java program to demonstrate example of yield() method)

/*  We will use Thread class methods so we are 
    importing the package but it is not mandate because 
    it is imported by default
*/
import java.lang.Thread;

class MyThread extends Thread {
    // Override run() method of Thread class
    public void run() {
        for (int i = 0; i < 5; ++i) {

            // By calling yield() method means MyThread stop its 
            // execution and giving a chance to main thread
            Thread.yield();

            System.out.println("Thread started:" + Thread.currentThread().getName());
        }
        System.out.println("Thread ended:" + Thread.currentThread().getName());
    }
}

class Main {
    public static void main(String[] args) {
        // Here we are calling start() method of Thread class and 
        // it will call run() method of MyThread
        MyThread mt = new MyThread();
        mt.start();

        for (int i = 0; i < 5; ++i) {
            System.out.println("Thread started:" + Thread.currentThread().getName());
        }
        System.out.println("Thread ended:" + Thread.currentThread().getName());
    }
}

Note:

注意:

  • If we comment this line /*Thread.yield()*/ then both thread will execute simultaneously we can't expect which thread will complete its execution.

    如果我们在/*Thread.yield()*/这一行中添加注释,则两个线程将同时执行,我们无法预期哪个线程将完成其执行。

  • If we don't comment this line /*Thread.yield()*/ then there is a possibility to execute main thread first because MyThread always calls yield() method.

    如果我们不注释/*Thread.yield()*/这一行,那么就有可能首先执行主线程,因为MyThread总是调用yield()方法。

Output

输出量

E:\Programs>javac Main.java

E:\Programs>java Main
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread started:Thread-0
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread ended:Thread-0


翻译自: https://www.includehelp.com/java/thread-class-static-native-void-yield-method-with-example.aspx

yield java示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值