//java中的线程是并发的,但事实也并非如此,线程分为10个等级 min_priority,max_priority,normal_priority 最低=0,最高=10,中等=5。
在创建对象后可以线程的优先级设置,用setPriority,
Java中比较特殊的线程被叫做守护线程,他的级别最低。用于为系统中的其他对象和线程服务。讲一个线程设置为守护线程的方式是在线程对象创建之前调用线程对象setDaemon方法。典型的是JVM.资源回收线程。始终在最低级的状态作为监控和管理。
public class PropertyThread {
/**
* @param 线程的优先级别
*/
public static void main(String[] args) {
TestPropertyThread tpt1 = new TestPropertyThread("Thread1");
tpt1.setPriority(Thread.MAX_PRIORITY);
tpt1.start();
TestPropertyThread tpt2 = new TestPropertyThread("Thread2");
tpt2.setPriority(Thread.MIN_PRIORITY);
tpt2.start();
TestPropertyThread tpt3 = new TestPropertyThread("Thread3");
tpt3.setPriority(Thread.NORM_PRIORITY);
tpt3.start();
}
}
class TestPropertyThread extends Thread {
public TestPropertyThread(String name ){
super(name +"创建线程对象啦");
}
public void run (){
for (int i=1;i<=3;i++){
System.out.println("重写了线程的run方法"+i+this.getName()+this.getPriority());
}
}
结果:
【重写了线程的run方法1Thread1创建线程对象啦10
重写了线程的run方法2Thread1创建线程对象啦10
重写了线程的run方法1Thread2创建线程对象啦1
重写了线程的run方法1Thread3创建线程对象啦5
重写了线程的run方法2Thread2创建线程对象啦1
重写了线程的run方法3Thread1创建线程对象啦10
重写了线程的run方法3Thread2创建线程对象啦1
重写了线程的run方法2Thread3创建线程对象啦5
重写了线程的run方法3Thread3创建线程对象啦5】