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示例