17,优先级 yeiled方法

 

package songyan;
/*
 * 线程组(很少使用):一般线程是由谁创建的就是那个组
 * 例:主线程创建t1,t2,那么t1,t2的组就是main
 * 组也可以修改,使用ThreadGroup
 * 
 * 优先级(Priority):抢资源的频率
 * 线程的优先级:1-10
 * t1.setPriority(10);
 * 三个常量:
 * 1:MIN_PRIORITY
 * 10:MAX_PRIORITY
 * 5:NORM_PRIORITY
 * 
 * 共享变量用静态
 * 固定数据用常量
 * 
 * yield()【用的很少,面试题会出】:强制释放执行权(临时停止)
 * 暂停当前执行的线程对象
 * 现象:稍微减缓线程的运行,达到所有线程都能执行到的效果
 * 
 */
class Demo implements Runnable{
    public void run()
    {
        while(true)
        {
                for( int i=0;i<60;i++)
                {
                    System.out.println(Thread.currentThread().getName()+"***"+i);
                    Thread.yield();
                }
        }
        
    }
}
public class test{
    public static void main(String[] args) {
        Demo d= new Demo();
        Thread t1= new Thread(d);
        Thread t2 = new Thread(d);
//        t1.setPriority(10);        
        t1.start();
        t2.start();
    }
}

 

转载于:https://www.cnblogs.com/exexex/p/8435400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值