首页 > 编程语言 > 详细

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

时间:2015-03-09 16:22:33      阅读:210      评论:0      收藏:0      [点我收藏+]

1、yield方法

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

2、创建同优先级的使用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();
  }
 }
}

2、测试类

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次运行

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

原文:http://my.oschina.net/u/2320342/blog/384345

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!