一、线程优先级和常用AP
每个线程执行时都有一定的优先级,优先级高的获取较多的执行机会,优先级低的线程则较少。线程默认的优先级与父线程优先级相同,Thread类提供了setPriority(int newPriority)getPriority()方法来设置和返回指定线程的优先级,其中setPriority()方法的参数范围在1-10之间,也可以使用Thread类的三个常量(如下),这三个静态常量来设置优先级可以保证程序会有更好的可移植性。
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;
常用API有:
int getPriority() : 返回线程的优先级
void setPriority() : 设置优先级
Thread.State getState() : 返回线程的当前状态
boolean isAlive() : 是否是活动状态
void setDaemon() : 设置线程为守护线程,当Java虚拟机退出时,所有的守护线程都终止
boolean isDaemon() : 判断当前