优先级越高,获得Cpu执行的时间片就会越多
Thread.MAX_PRIORITY
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
示例:
public class test {
public static void main(String[]args){
Thread t1 = new Thread(new Thread1());
Thread t2 = new Thread(new Thread2());
t1.setPriority(Thread.NORM_PRIORITY +3);
t1.start();
t2.start();
}
}
class Thread1 implements Runnable {
public void run() {
for(int i = 0; i<1000;i++){
System.out.println("Thread1 :"+i);
}
}
}
class Thread2 implements Runnable {
public void run() {
for(int i = 0; i<1000;i++){
System.out.println("============Thread2:"+i);
}
}
}
返回的是一个Thread对象的引用
this是返回当前对象
class MyThread implements Runnable {
public void run() {
System.out.println(Thread.currentThread().isAlive());
for(int i= 0 ;i < 50;i++){
System.out.println("The MyThread:"+ i);
}
}
}
判断当前线程是否正在执行
注意优先级是概率而非先后顺序(优先级高可能会执行时间长,但也不一定)