java yield的简单使用

package yi.test01;


/**
 * 2016年9月13日10:59:07
 * @author yangwl
 * 
 * yield
 * 休眠当前线程, 是线程编程可执行状态,以便其他相同优先级的线程有机会执行,
 *  注意: 再次执行调度程序决定的, 可以立刻会再次执行。。。
 *  
 *  yield会释放资源锁吗?
 *     yield不会释放资源所,当暂停后,等待调度程序再次调用,走完同步区域代码后才释放锁
 *  
 */
public class Test03 implements Runnable{


private String name;     
   
   private Test03(String name) {     
       this.name = name;     
   }  

public synchronized void run() {
System.out.println(name + " -> Start.");
for(int i = 0; i < 100; i++) {
if(i == 50) {
Thread.yield();
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
System.out.println(Thread.currentThread().getName() + "'s i=" + (i+1));
}
System.out.println(name + "X -> End.");
}

public static void main(String[] args) {
Test03 t1 = new Test03("A");
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t1);

thread1.start();
thread2.start();
}


}


A -> Start.
Thread-0's i=1
Thread-0's i=2
Thread-0's i=3
Thread-0's i=4
Thread-0's i=5
Thread-0's i=6
Thread-0's i=7
Thread-0's i=8
Thread-0's i=9
Thread-0's i=10
Thread-0's i=11
Thread-0's i=12
Thread-0's i=13
Thread-0's i=14
Thread-0's i=15
Thread-0's i=16
Thread-0's i=17
Thread-0's i=18
Thread-0's i=19
Thread-0's i=20
Thread-0's i=21
Thread-0's i=22
Thread-0's i=23
Thread-0's i=24
Thread-0's i=25
Thread-0's i=26
Thread-0's i=27
Thread-0's i=28
Thread-0's i=29
Thread-0's i=30
Thread-0's i=31
Thread-0's i=32
Thread-0's i=33
Thread-0's i=34
Thread-0's i=35
Thread-0's i=36
Thread-0's i=37
Thread-0's i=38
Thread-0's i=39
Thread-0's i=40
Thread-0's i=41
Thread-0's i=42
Thread-0's i=43
Thread-0's i=44
Thread-0's i=45
Thread-0's i=46
Thread-0's i=47
Thread-0's i=48
Thread-0's i=49
Thread-0's i=50
Thread-0's i=51
Thread-0's i=52
Thread-0's i=53
Thread-0's i=54
Thread-0's i=55
Thread-0's i=56
Thread-0's i=57
Thread-0's i=58
Thread-0's i=59
Thread-0's i=60
Thread-0's i=61
Thread-0's i=62
Thread-0's i=63
Thread-0's i=64
Thread-0's i=65
Thread-0's i=66
Thread-0's i=67
Thread-0's i=68
Thread-0's i=69
Thread-0's i=70
Thread-0's i=71
Thread-0's i=72
Thread-0's i=73
Thread-0's i=74
Thread-0's i=75
Thread-0's i=76
Thread-0's i=77
Thread-0's i=78
Thread-0's i=79
Thread-0's i=80
Thread-0's i=81
Thread-0's i=82
Thread-0's i=83
Thread-0's i=84
Thread-0's i=85
Thread-0's i=86
Thread-0's i=87
Thread-0's i=88
Thread-0's i=89
Thread-0's i=90
Thread-0's i=91
Thread-0's i=92
Thread-0's i=93
Thread-0's i=94
Thread-0's i=95
Thread-0's i=96
Thread-0's i=97
Thread-0's i=98
Thread-0's i=99
Thread-0's i=100
AX -> End.
A -> Start.
Thread-1's i=1
Thread-1's i=2
Thread-1's i=3
Thread-1's i=4
Thread-1's i=5
Thread-1's i=6
Thread-1's i=7
Thread-1's i=8
Thread-1's i=9
Thread-1's i=10
Thread-1's i=11
Thread-1's i=12
Thread-1's i=13
Thread-1's i=14
Thread-1's i=15
Thread-1's i=16
Thread-1's i=17
Thread-1's i=18
Thread-1's i=19
Thread-1's i=20
Thread-1's i=21
Thread-1's i=22
Thread-1's i=23
Thread-1's i=24
Thread-1's i=25
Thread-1's i=26
Thread-1's i=27
Thread-1's i=28
Thread-1's i=29
Thread-1's i=30
Thread-1's i=31
Thread-1's i=32
Thread-1's i=33
Thread-1's i=34
Thread-1's i=35
Thread-1's i=36
Thread-1's i=37
Thread-1's i=38
Thread-1's i=39
Thread-1's i=40
Thread-1's i=41
Thread-1's i=42
Thread-1's i=43
Thread-1's i=44
Thread-1's i=45
Thread-1's i=46
Thread-1's i=47
Thread-1's i=48
Thread-1's i=49
Thread-1's i=50
Thread-1's i=51
Thread-1's i=52
Thread-1's i=53
Thread-1's i=54
Thread-1's i=55
Thread-1's i=56
Thread-1's i=57
Thread-1's i=58
Thread-1's i=59
Thread-1's i=60
Thread-1's i=61
Thread-1's i=62
Thread-1's i=63
Thread-1's i=64
Thread-1's i=65
Thread-1's i=66
Thread-1's i=67
Thread-1's i=68
Thread-1's i=69
Thread-1's i=70
Thread-1's i=71
Thread-1's i=72
Thread-1's i=73
Thread-1's i=74
Thread-1's i=75
Thread-1's i=76
Thread-1's i=77
Thread-1's i=78
Thread-1's i=79
Thread-1's i=80
Thread-1's i=81
Thread-1's i=82
Thread-1's i=83
Thread-1's i=84
Thread-1's i=85
Thread-1's i=86
Thread-1's i=87
Thread-1's i=88
Thread-1's i=89
Thread-1's i=90
Thread-1's i=91
Thread-1's i=92
Thread-1's i=93
Thread-1's i=94
Thread-1's i=95
Thread-1's i=96
Thread-1's i=97
Thread-1's i=98
Thread-1's i=99
Thread-1's i=100
AX -> End.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值