Thread 类提供了多种常用方法,用于创建和管理线程。下面是一些常用的 Thread 方法及其说明:


1. 创建和启动线程的方法

(1) start()

  • 作用:启动线程。调用 start() 方法后,线程将进入“可运行”状态,并开始执行 run() 方法。
  • 注意:只能调用一次,调用多次会抛出 IllegalThreadStateException 异常。
1
2
Thread thread = new Thread(() -> System.out.println("线程正在执行"));
thread.start();

(2) run()

  • 作用:线程执行的代码逻辑。run() 方法是线程的入口,线程在启动时会执行 run() 方法中的代码。
  • 注意:通常不直接调用 run() 方法,而是调用 start() 来启动线程。
1
2
3
4
5
6
7
8
9
10
11
12
13
class MyThread extends Thread {
@Override
public void run() {
System.out.println("线程执行");
}
}

public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start(); // 调用 start() 执行 run()
}
}

2. 线程的生命周期方法

(1) sleep(long millis)

  • 作用:使当前线程暂停(休眠)指定时间(毫秒)。休眠期间线程不占用 CPU 资源。
  • 注意sleep() 方法是静态的,调用后,当前线程进入“休眠”状态。
1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("线程开始");
Thread.sleep(2000); // 当前线程休眠 2 秒
System.out.println("线程结束");
}
}

(2) interrupt()

  • 作用:中断当前线程,设置线程的中断标志为 true
  • 注意:如果线程在 sleep()wait()join() 等方法上被阻塞,调用 interrupt() 会抛出 InterruptedException
1
2
3
4
5
6
7
8
9
10
11
Thread thread = new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("线程被中断");
}
});
thread.start();

// 中断线程
thread.interrupt();

3. 获取线程状态和信息的方法

(1) getId()

  • 作用:返回当前线程的唯一标识符(ID)。
  • 返回类型long
1
2
Thread thread = new Thread();
System.out.println("线程ID: " + thread.getId());

(2) getName()

  • 作用:返回当前线程的名称。默认名称是 Thread-0Thread-1 等,可以通过构造方法修改。
1
2
Thread thread = new Thread();
System.out.println("线程名称: " + thread.getName());

(3) getState()

  • 作用:获取当前线程的状态。返回值是 Thread.State 枚举类型,可能的状态有:
    • NEW:线程已创建,但未启动。
    • RUNNABLE:线程可以执行(即使线程正在等待 CPU 时间片)。
    • BLOCKED:线程在等待锁。
    • WAITING:线程正在等待其他线程的通知。
    • TIMED_WAITING:线程在等待指定时间后继续执行。
    • TERMINATED:线程已完成执行。
1
2
Thread thread = new Thread();
System.out.println("线程状态: " + thread.getState());

(4) isAlive()

  • 作用:判断线程是否仍在运行。返回 true 如果线程已启动并且尚未终止。
1
2
3
Thread thread = new Thread();
thread.start();
System.out.println("线程是否活着: " + thread.isAlive());

(5) setName(String name)

  • 作用:设置当前线程的名称。
  • 说明:默认线程名称是 Thread-0Thread-1 等,可以通过 setName() 方法修改线程名称,便于识别和调试。
1
2
3
Thread thread = new Thread();
thread.setName("自定义线程名称");
System.out.println("当前线程名称: " + thread.getName());

4. 线程的控制方法

(1) join()

  • 作用:使当前线程等待目标线程完成(终止),即当前线程会阻塞直到目标线程终止为止。
  • 注意join() 可以带上等待时间,表示等待目标线程最多执行指定的时间。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Thread thread1 = new Thread(() -> {
System.out.println("线程1执行");
});
Thread thread2 = new Thread(() -> {
System.out.println("线程2执行");
});

thread1.start();
thread2.start();

try {
thread1.join(); // 等待线程1结束
thread2.join(); // 等待线程2结束
} catch (InterruptedException e) {
e.printStackTrace();
}

5. 线程的优先级方法

(1) setPriority(int newPriority)

  • 作用:设置线程的优先级。线程优先级是一个整数,范围从 Thread.MIN_PRIORITY(1)到 Thread.MAX_PRIORITY(10),默认优先级是 Thread.NORM_PRIORITY(5)。
1
2
Thread thread = new Thread();
thread.setPriority(Thread.MAX_PRIORITY); // 设置为最大优先级

(2) getPriority()

  • 作用:获取当前线程的优先级。
1
2
Thread thread = new Thread();
System.out.println("线程优先级: " + thread.getPriority());

6. 其他常用线程方法

(1) yield()

  • 作用:使当前线程主动让出 CPU 时间片,允许其他线程有机会执行。它并不会挂起线程,只是让当前线程回到可运行状态,其他线程可能获得执行机会。
1
Thread.yield(); // 当前线程让步

(2) currentThread()

  • 作用:返回当前正在执行的线程对象。
1
2
Thread currentThread = Thread.currentThread();
System.out.println("当前线程名称: " + currentThread.getName());

7.Thread 常用方法总结

方法 作用
start() 启动线程并执行 run() 方法
run() 线程的执行逻辑
sleep(long millis) 使线程休眠指定时间
interrupt() 中断线程
getId() 获取线程的唯一标识符
getName() 获取线程的名称
setName(String name) 设置线程的名称
getState() 获取线程的当前状态
isAlive() 判断线程是否仍在运行
join() 当前线程等待目标线程完成
setPriority(int) 设置线程的优先级
getPriority() 获取线程的优先级
yield() 当前线程让出 CPU 时间片
currentThread() 获取当前正在执行的线程对象