Java线程优先级

一、优先级
优先级指的是越高的优先级,越有可能执行。在Thread类提供以下两个方法进行优先级操作:
*设置优先级public final void setPriority(int newPriority)
*取得优先级public final int getPriority()
设置和取得优先级都是使用了int数据类型,对此有三中取值:
*最高优先级:public static final int MAX_PRIORITY
*中等优先级:public static final int NORM_PRIORITY
*最低优先级:public static final int MIN_PRIORITY

public class MyThread implements Runnable    {

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int t = 0 ; t < 100 ; t--) {
            System.out.println(Thread.currentThread().getName() + "t=" + t);
        }

    }

}
public class TestDemo {
    public static void main(String[] args)  {
        MyThread mt = new MyThread();
        Thread t1 = new Thread(mt,"自己的线程对象A");
        Thread t2 = new Thread(mt,"自己的线程对象B");
        Thread t3 = new Thread(mt,"自己的线程对象c");
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }


}

———————————————————————总结:
1、Thread.currentThread 可以取得当前线程类对象
2、Thread.sleep()主要是休眠,感觉是一起休眠,实际上是有先后顺序的
3、优先级越高的线程对象,越有可能限制性
4、主线程属于zhong中等优先级

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值