类的定义:
public class Thread implements Runnable {}
构造方法
1、无参构造方法
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
此无参构造方法会创一个默认线程名为“Thread-编号”的线程,编号是static静态常量,默认0,然后每次创建都会增加1
2、有参构造
//构建带有线程名称的线程
public Thread(String name) {
init(null, null, name, 0);
}
//构建Runnable实例的线程,线程名默认
//注意Thread类也为Runnable实例,因此,可以将一个线程设置为另一个线程的子线程
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
//构建Runnable实例和线程名称的线程
public Thread(Runnable target, String name) {
init(null, target, name, 0);
}
3、常用方法
- sleep方法(本地方法),接受传入毫秒数,此方法抛出中断异常,中断异常不属于RuntimeException异常,所以需要在调用方法中去处理异常。阻塞方法,其到时间后会自动恢复就绪状态,如果线程中断就会抛出InterruptedException异常
public static native void sleep(long millis) throws InterruptedException;
- yield方法(本地方法),线程礼让,但是并不代表一定会让给其他线程,仅仅是给线程调度器的一个暗示,调度器是否执行就看调度器的心情了。
public static native void yield();
- start方法(启动线程)
public synchronized void start() {
//线程状态,不为0代表已经启动了,会执出非法异常
if (threadStatus != 0)
throw new IllegalThreadStateException();
//将线程加入线程调
group.add(this);
//启动前启动标志false
boolean started = false;
try {
//关键方法
start0();
//启动标志true
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
//本地方法,由当前操作系统实现
private native void start0();
- run方法,start启动后实际执行的主体方法,如果线程构造传入Runnable实例,则执行的是Runnable实例中的run方法,否则是需要由其子类实现此方法。
public void run() {
if (target != null) {
target.run();
}
}
- interrupt中断方法,中断线程,将其置就绪状态,置中标标识符为true,等待调度器的分配 ,中断可以中断正在运行的线程,也可以中断阻塞状态,中断阻塞状态时会产生InterruptedException中断异常。
public void interrupt()
- interrupted和isInterrupted方法,两个主要区别在于是否会重置中断状态
//静态方法,调用的当前线程的isInterrupted(),并且会重置当前线程的中断状态
//重置后再次调用会返回false
//if线程中断,第一次a.interrupted(),返回true,并且重置中断状态
//所以第二次a.interrupted()会返回false
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
//普通方法,线程是否中断,不会重置线程的中断状态
public boolean isInterrupted() {
return isInterrupted(false);
}
//本地方法
private native boolean isInterrupted(boolean ClearInterrupted);
有个很有特点的程序,子线程启动后,将主线程休眠1秒,然后子线程中断,两次判断子线程的中断标识。
首先呢,子线程和主线程都已经启动了,然后主线程休眠1秒,1s太长了,所以子线程差不多已经执行完了,执行完了也就没有中断了,如果中主线程休眠的时间减小到1毫秒,多次测试结果可能会出现一个true,其实子线程和主线程的休眠无关的,如果在休眠期子线程执行完了,也就无所谓中断了,如果没有运行完,就会继续和主结程抢占时间段。
t1.start();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
System.out.println(t1.isInterrupted());
System.out.println(t1.isInterrupted());
- join方法,强制线程,调用join方法的线程会一直执行到终止(die):Waits for this thread to die.当前线程会一直等待强制线程执行完毕,或者当前线程等待被打断,产生InterruptedException异常
public final void join() throws InterruptedException {
join(0);
}
- wait():等待,强制阻塞,不能自动唤醒,需要相关的唤醒方法
此方法位于Object类之中
//本地方法,参数为最大等待时间
public final native void wait(long timeout) throws InterruptedException;
- notify()和notifyAll():唤醒休眠的线程,等待和唤醒需要成对使用
此方法位于Object类之中
public final native void notify();
public final native void notifyAll();
- 优先级的操作方法:setPriority和getPriority
public final void setPriority(int newPriority){
ThreadGroup g;
checkAccess();
//默认优先级范围在1---10之间
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
//设置的优先级不能超过线程组的优先级
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
其中newPriority Thread类默认定义了三种参数:
MIN_PRIORITY = 1;NORM_PRIORITY = 5;MAX_PRIORITY = 10
通过源代码可知,优先级是在[1---10]之间
优先级高并不代表一定会先运行,只是说优先级高的先执行的概率要比优先级低的高