线程优先级:线程优先级使用setPriority()方法,放在Start()方法前面使用。 优先级为1~10的数字中间,不可超过这个区间。
public class PriorityYest implements Runnable{ public static void main(String[] args) { PriorityYest w=new PriorityYest(); Thread t1=new Thread(w); Thread t2=new Thread(w); Thread t3=new Thread(w); Thread t4=new Thread(w); Thread t5=new Thread(w); Thread t6=new Thread(w); t1.setPriority(Thread.MAX_PRIORITY);//MAX表示最大 t6.setPriority(Thread.MIN_PRIORITY);//MIN表示最小 t3.setPriority(Thread.NORM_PRIORITY);//NORM表示中间 t5.setPriority(Thread.MAX_PRIORITY); t4.setPriority(Thread.MAX_PRIORITY); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); } }Thread.MAX_PRIORITY表示数字10; Thread.MIN_PRIORITY表示数字1; Thread.NORM_PRIORITY表示数字5,如果没有设置,则默认值为5。 守护线程:setDaemon(true)方法,设置一个线程为守护线程,虚拟机不用等待线程执行完毕,就可以结束运行,默认方法为false。
public class DaemonTest { public static void main(String[] args) { bless w=new bless(); Thread w1=new Thread(w); w1.setDaemon(false); w1.start(); new Thread(new you()).start(); } } class you implements Runnable{ @Override public void run() { for(int i=0;i<365;i++) { System.out.println("happy life"); } } } class bless implements Runnable{ @Override public void run() { for(;true;) { System.out.println("bless you"); } } }