6.线程优先级:Thread中有哪几个线程优先级常量???线程优先级相关常用方法有哪几个???
学习:第7遍
1.Thread中有哪几个线程优先级常量???
线程优先级范围[1,10], 优先级越高,执行机会越大,只是概率,并不一定
最大优先级即10:Thread.MAX_PRIORITY
最小优先级即1:Thread.MIN_PRIORITY
普通优先级即5:Thread.NORM_PRIORITY
2.线程优先级相关常用方法有哪几个???
方法一:thread.setPriority(10)
作用:设置线程优先级
方法二:thread.getPriority()
作用:得到线程优先级
方法三:Thread.currentThread()
作用:得到当前线程,静态方法
方法四:System.currentTimeMillis()
作用:得到系统的当前时间,long型表示,静态方法
/*
* 线程优先级
* 范围[1,10]
* 优先级越高,执行机会越大,只是概率,并不一定
*/
public class Test{
public static void main(String[] args) {
// System.out.println("最大优先级:"+Thread.MAX_PRIORITY);
// System.out.println("最小优先级:"+Thread.MIN_PRIORITY);
// System.out.println("普通优先级:"+Thread.NORM_PRIORITY);
// System.out.println(Thread.currentThread().getPriority());
for (int i = 1; i <= 20; i++) {
MyThread3 t1 = new MyThread3();
MyThread3 t2 = new MyThread3();
t1.setPriority(10);
t2.setPriority(1);
t1.start();
t2.start();
}
}
}
class MyThread3 extends Thread {
@Override
public void run() {
long start = System.currentTimeMillis();
String str = "";
for (int i = 1; i <= 10000; i++) {
str += i;
}
long end = System.currentTimeMillis();
System.out.println(getPriority() + "-----------" + (end - start) + "ms");
}
}