码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-09 16:25:39      阅读:258      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!