线程调度三(yield方法的使用)

一、yield方法java

    注:yield方法被调用后,并非让当前线程转入被阻塞状态,而是转入可运行状态app

二、建立同优先级的使用yield方法的类测试

package com.ljb.app.thread;
/**
 * 第一个线程(使用yield方法)
 * @author LJB
 * @version 2015年3月9日
 */
public class OneYield extends Thread{
 public void run () {
  for (int i = 0 ;  i < 5 ; i++) {
   System.out.println("oneYield第" + (i+1) + "次运行");
   Thread.yield();
  }
 }
}
package com.ljb.app.thread;
/**
 * 第二个线程(使用yield方法)
 * @author LJB
 * @version 2015年3月9日
 */
public class TwoYield extends Thread{
 public void run () {
  for (int i = 0 ;  i < 5 ; i++) {
   System.out.println("twoYield第" + (i+1) + "次运行");
   Thread.yield();
  }
 }
}

二、测试类spa

package com.ljb.app.thread;
/**
 * 测试yield方法
 * @author LJB
 * @version 2015年3月9日
 */
public class TestYield {
 /**
  * @param args
  */
 public static void main(String[] args) {
  Thread oneTh = new OneYield();
  Thread twoTh = new TwoYield();
  
  oneTh.start();
  twoTh.start();
 }
}

运行结果:线程

oneYield第1次运行
twoYield第1次运行
oneYield第2次运行
twoYield第2次运行
oneYield第3次运行
twoYield第3次运行
oneYield第4次运行
twoYield第4次运行
oneYield第5次运行
twoYield第5次运行
code

相关文章
相关标签/搜索