您的位置:首页 > 编程语言 > Java开发

Java线程优先级

2016-07-26 23:40 375 查看
一、优先级

优先级指的是越高的优先级,越有可能执行。在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中等优先级
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java