thread类的yield()方法使当前正在执行的线程对象暂时暂停并允许其他线程执行。
用法
public static void yield()
返回
此方法不返回任何值。
示例
public class JavaYieldExp extends Thread
{
public void run()
{
for (int i=0; i<3 ; i++)
System.out.println(Thread.currentThread().getName() + " in control");
}
public static void main(String[]args)
{
JavaYieldExp t1 = new JavaYieldExp();
JavaYieldExp t2 = new JavaYieldExp();
// this will call run() method
t1.start();
t2.start();
for (int i=0; i<3; i++)
{
// Control passes to child thread
t1.yield();
System.out.println(Thread.currentThread().getName() + " in control");
}
}
}
输出:
main in control main in control main in control Thread-0 in control Thread-0 in control Thread-0 in control Thread-1 in control Thread-1 in control Thread-1 in control
相关用法
- Java Thread toString()用法及代码示例
- Java Thread interrupted()用法及代码示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代码示例
- Java Thread suspend()用法及代码示例
- Java Thread destroy()用法及代码示例
- Java Thread holdLock()用法及代码示例
- Java Thread getContextClassLoader()用法及代码示例
- Java Thread setContextClassLoader()用法及代码示例
- Java Thread sleep()用法及代码示例
- Java Thread getThreadGroup()用法及代码示例
- Java Thread isInterrupted()用法及代码示例
- Java Thread enumerate()用法及代码示例
- Java Thread notify()用法及代码示例
- Java Thread resume()用法及代码示例
- Java Thread activeCount()用法及代码示例
- Java Thread isDaemon()用法及代码示例
- Java Thread setDaemon()用法及代码示例
- Java Thread getId()用法及代码示例
- Java Thread setName()用法及代码示例
- Java Thread getDefaultUncaughtExceptionHandler()用法及代码示例
注:本文由纯净天空筛选整理自 Java Thread yield() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。