| function_name | static | 功能说明 | 注意 |
|---|---|---|---|
| start() | 否 | 启动一个线程,在新线程中执行run方法中的代码 | start方法只是让线程就绪,并不是马上执行(CPU时间片还没分配给它),start方法只能调用一次,再次调用会报IllegalThreadStateException |
| run() | 否 | 新线程启动之后会调用的方法 | 如果在Thread的构造方法传入了Runnable参数,则线程启动执行Runnable的run方法,否则不执行任何操作。可以继承Thread来重写run方法,覆盖默认行为 |
| join() | 否 | 等待线程运行结束 | |
| join(long n) | 否 | 等待线程运行结束,最多等待n毫秒 | |
| getId() | 否 | 获取线程的长整型id | id唯一 |
| getName() | 否 | 获取线程名 | |
| setName(String) | 否 | 设置线程名 | |
| getPriority() | 否 | 获取线程优先级 | |
| setPriority(int) | 否 | 设置线程优先级 | java规定线程优先级是1-10的整数,数值越大优先级越高,被cpu调度的概率越大 |
| getState() | 否 | 获取线程状态 | 线程状态有6中,定义在枚举中:NEW、RUNNABLE、BLOCKED、WAIT、TIMED_WAIT、TERMINATED |
| isAlive() | 否 | 线程是否存活(尚未运行完) | |
| interruput() | 否 | 给线程添加中断标记 | RUNNABLE、BLOCKED状态的线程只是添加中断标记,不会改变状态。WAITING、TIMED_WAITING状态的线程会抛出异常InterruptedException,并将线程状态改为RUNNABLE,同时清除中断标记 |
| isInterrupted() | 否 | 判断当前线程的中断标记,不会清除标记 | |
| interrupted() | 是 | 判断当前线程的中断标记,会清除标记 | |
| currentThread() | 是 | 获取当前正在运行的线程对象 | |
| sleep(long n) | 是 | 让当前正在运行的线程休眠n毫秒,让出cpu时间片,但不会释放锁 | |
| yield() | 是 | 让出cpu时间片,进入就绪状态,重新等待调度,会释放锁 | 主要用于测试和调试 |
start可以启动一个线程,run里面操作可以看做线程的任务。直接调用run方法,会在主线程执行run里面的操作,这块比较简单就不写demo了。
sleep:
演示代码:
public class SleepAndYield {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
try {
// Thread.sleep(2000);
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1");
System.out.println(t1.getState());
t1.start();
System.out.println(t1.getState());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t1.getState());
t1.interrupt();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t1.getState());
}
}
yield:
演示代码:
public class SleepAndYield1 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
int count =0;
while (true){
System.out.println("t1----->"+count++);
}
},"t1");
Thread t2 = new Thread(() -> {
int count =0;
while (true){
Thread.yield();
System.out.println(" t2----->"+count++);
}
},"t2");
t1.start();
t2.start();
}
}
无锁下相同:
有锁下不同:
join方法等待所属线程对象的线程执行结束,会阻塞调用它的线程
演示代码:
public class Join {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> sleep(1), "t1");
Thread t2 = new Thread(() -> sleep(2), "t2");
t1.start();
t2.start();
long start =System.currentTimeMillis();
t1.join();
t2.join();
long end =System.currentTimeMillis();
System.out.println(end-start);
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
限时等待
public class JoinN {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> sleep(2), "t1");
t1.start();
long start =System.currentTimeMillis();
t1.join(1000);
long end =System.currentTimeMillis();
System.out.println(end-start);
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
RUNNABLE、BLOCKED状态的线程只是添加中断标记,不会改变状态。WAITING、TIMED_WAITING状态的线程会抛出异常InterruptedException,并将线程状态改为RUNNABLE,同时清除中断标记。
打断WAIT、TIMED_WAIT线程:
public class Interrupt1 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> sleep(2), "t1");
t1.start();
sleep(1);
t1.interrupt();
System.out.println("打断标记:" + t1.isInterrupted());
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
打断RUNNABLE线程:
不能让t1线程停止运行
public class Interrupt2 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true){
}
}, "t1");
t1.start();
sleep(1);
t1.interrupt();
System.out.println("打断标记:" + t1.isInterrupted());
}
加判断让t1线程停止运行
public class Interrupt2 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if(Thread.interrupted()){
System.out.println("被打断了...");
break;
}
}
}, "t1");
t1.start();
sleep(1);
t1.interrupt();
//输出false,因为interrupted()会清除打断标记
System.out.println("打断标记:" + t1.isInterrupted());
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Interrupt3 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
while (true) {
System.out.println("执行监控");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//二阶段打断
Thread.currentThread().interrupt();
}
if(Thread.interrupted()){
System.out.println("被打断了...");
break;
}
}
}, "t1");
t1.start();
Thread.sleep(3500);
//一阶段打断,t1线程正好在睡眠中,标记的打断会被清除,因此需要在catch块添加二阶段打断
t1.interrupt();
System.out.println("打断标记:" + t1.isInterrupted());
}
}
public class Interrupt4 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
System.out.println("before park");
LockSupport.park();
System.out.println("after interrupt");
//被标记打断的线程,再次park,无法暂停
LockSupport.park();
System.out.println("can't park");
}, "t1");
t1.start();
Thread.sleep(1000);
t1.interrupt();
System.out.println("结束");
}
}
setDaemon将设置线程为守护线程,顾名思义守护其他线程的线程,当没有线程需要守护,自己就强制结束了。
public class SetDaemon {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
System.out.println("Daemon main thread");
}
}, "t1");
t1.setDaemon(true);
t1.start();
sleep(1);
System.out.println("main thread over!");
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
OS层面有以下五种状态:初始状态–>可运行–>运行中–>阻塞–>终止

Java语言中,在Thread类定义了一个状态枚举,表示线程的状态,有以下六种:NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED。看下六种状态对应的代码
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
}, "t1");
Thread t2 = new Thread(() -> {
while (true) {
}
}, "t2");
t2.start();
Thread t3 = new Thread(() -> {
synchronized (ThreadState.class) {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t3");
t3.start();
Thread t4 = new Thread(() -> sleep(1000), "t4");
t4.start();
Thread t5 = new Thread(() -> {
synchronized (ThreadState.class) {
}
}, "t5");
t5.start();
Thread t6 = new Thread(() -> {
}, "t6");
t6.start();
Thread.sleep(2);
System.out.println("t1:" + t1.getState());
System.out.println("t2:" + t2.getState());
System.out.println("t3:" + t3.getState());
System.out.println("t4:" + t4.getState());
System.out.println("t5:" + t5.getState());
System.out.println("t6:" + t6.getState());
}
private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
再看下状态转换图简易版


如图所示:开始和终止状态一样的,java中将就绪和运行合并为可运行,将阻塞细分为阻塞、等待和超时等待。值得注意的是,操作系统中一部分阻塞,在java中认为是可运行状态,例如IO阻塞。