Java中Thread类的基本用法总结
在Java中,Thread
类是实现多线程编程的核心。本文将从线程创建、线程中断、线程等待、线程休眠和获取线程实例五个方面,总结其基本用法。
一.线程的创建
方式一:继承Thread
类,重写run方法
package thread;
class MyThread extends Thread{
@Override
public void run() {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Demo1 {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread();
t.start();
while (true){
System.out.println("hello main");
t.sleep(1000);
}
}
}
方式二:实现 Runnable, 重写 run
package thread;
public class Demo3 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
方式三:继承 Thread, 重写 run, 使用匿名内部类
package thread;
public class Demo3 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
方式四:实现 Runnable, 重写 run, 使用匿名内部类
package thread;
public class Demo4 {
public static void main(String[] args) {
Thread t = new Thread(new Runnable(){
@Override
public void run() {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
方式五:使用Lambda表达式(推荐使用)
package thread;
public class Demo5 {
public static void main(String[] args) {
Thread t = new Thread(() ->{
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
二.线程中断
thread收到通知的方式有两种:
1.线程主动检查中断标志
线程通过 isInterrupted()
主动检查中断标志,适合非阻塞任务。
package thread;
// 线程的打断
public class Demo8 {
private static boolean isQuit = false;
public static void main(String[] args) throws InterruptedException {
// boolean isQuit = false;
Thread t = new Thread(() -> {
while (!isQuit) {
System.out.println("线程工作中");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程工作完毕!");
});
t.start();
Thread.sleep(5000);
isQuit = true;
System.out.println("设置 isQuit 为 true");
}
}
2.线程在阻塞操作中被中断
若线程在 sleep()
、wait()
等阻塞操作中被中断,会抛出 InterruptedException
,需在 catch
块中:
-
处理中断逻辑(如资源释放)。
-
调用
Thread.currentThread().interrupt()
恢复中断标志,避免丢失中断信号。
package thread;
public class Demo7 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
// Thread 类内部, 有一个现成的标志位, 可以用来判定当前的循环是否要结束.
while (!Thread.currentThread().isInterrupted()) {
System.out.println("线程工作中");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// 1. 假装没听见, 循环继续正常执行.
e.printStackTrace();
// 2. 加上一个 break, 表示让线程立即结束.
// break;
// 3. 做一些其他工作, 完成之后再结束.
// 其他工作的代码放到这里.
break;
}
}
});
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("让 t 线程终止. ");
t.interrupt();
}
}
三.线程的等待
有时候需要等待一个线程完成它的工作后,才能进行自己的下一步工作。
比如主线程中调用t.join(),此时就是让主线程等待t线程先结束,再执行主线程。
package thread;
public class Demo8 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(()->{
for (int i = 0; i <5;i++){
System.out.println("我先执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t1.start();
t1.join();
Thread t2 = new Thread(()->{
for (int j = 0; j <5;j++){
System.out.println("我最后执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t2.start();
}
}
四.线程休眠
上面代码其实也用到了线程的休眠
方法:public static void sleep(long millis) throws InterruptedException
这里的休眠当前线程millis毫秒
五.获取线程实例
使用Thread.currentThread();
Thread currentThread = Thread.currentThread();
System.out.println("当前线程名称:" + currentThread.getName());