join,yield

package duoxiancheng;
/*
 * 谁开启的线程,就属于哪个组;
 * 优先级:抢资源的频率;
 * 
 * 
 * 
 * */


class JoinDemo implements Runnable{
	private int i=0;
	public synchronized void run(){
		//try{wait();}catch(Exception e){}
		
		for(;i<1000;i++)
		{
			System.out.println(Thread.currentThread().toString()+"---thread---"+i);
			Thread.yield();//暂停此线程对象,临时释放用,可以减缓线程的运行,让其他线程都有机会运行;
		}
	}
}






public class Join {

	public static void main(String[] args) throws InterruptedException {
		JoinDemo jd=new JoinDemo();
		Thread t1=new Thread(jd);
		Thread t2=new Thread(jd);
		Thread t3=new Thread(jd);
		//t1.setDaemon(true);
		t1.setPriority(Thread.MAX_PRIORITY);//默认为5,数字越大优先级越高,其他线程也是抢的到执行权的;
		t2.setPriority(Thread.MIN_PRIORITY);
		t3.setPriority(Thread.NORM_PRIORITY);
		t1.start();
		//t1.join();//t1抢夺cpu执行权;t1结束了main才会获得执行资格(和其他线程一起抢执行权);main释放了执行权;
					//join可用来临时加入线程执行;
					//如果t1wait了,此时会抛出异常;
		
		t2.start();
		t3.start();
		/*
		for(int i=0;i<100;i++)
		{
			System.out.println(Thread.currentThread().toString()+"---main---"+i);
		}
		*/
		System.out.println("over");
		
		
		
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值