package cn.mb.thread;
/**
- yield礼让线程,暂停线程直接进入就绪状态不是阻塞状态
- @author lenovo
*/
public class YieldDemo {
public static void main(String[] args) {
MyYield my=new MyYield();
new Thread(my,“a”).start();
new Thread(my,“b”).start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"----->start");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"----->end");
}
}
package cn.mb.thread;
/**
- join合并线程,插队线程
- @author 骚彬
*/
public class BlockJoin {
public static void main(String[] args) {
System.out.println(“爸爸和儿子买烟的故事”);
new Thread(new Father()).start();
}
}
class Father extends Thread{
@Override
public void run() {
System.out.println(“想抽烟,发现没了”);
System.out.println(“让儿子去买中华”);
Thread t=new Thread(new Son()) ;
t.start();
try {
t.join();//Father被阻塞
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(“孩子丢了,找孩子”);
}
System.out.println(“接过烟,零钱给了儿子”);
}
}
class Son extends Thread{
@Override
public void run() {
System.out.println(“接过钱出去了。。。”);
System.out.println(“游戏厅玩10秒”);
for(int i=0;i<10;i++) {
System.out.println(i+“秒过去了—”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(“买烟。。。。”);
System.out.println(“手拿中华回家了”);
}
}
输出结果:
爸爸和儿子买烟的故事
想抽烟,发现没了
让儿子去买中华
接过钱出去了。。。
游戏厅玩10秒
0秒过去了—
1秒过去了—
2秒过去了—
3秒过去了—
4秒过去了—
5秒过去了—
6秒过去了—
7秒过去了—
8秒过去了—
9秒过去了—
买烟。。。。
手拿中华回家了
接过烟,零钱给了儿子