線程調度三(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

相關文章
相關標籤/搜索