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");
}
}
join,yield
最新推荐文章于 2024-03-30 10:30:00 发布