目录
一、什么是Thread类
Java 标准库中 Thread 类是Java中实现多线程编程的基础类, 可以视为是对操作系统提供的 API 进行了进一步的抽象和封装。
二、Thread类中常用的方法
1、线程创建的方法 :
①创建子类,继承自 Thread
class MyThread_1 extends Thread{
@Override
public void run() {
// run 方法描述了创建者个线程后,其所要做的任务
// 每个线程都是并发执行的(各自执行各自的代码),所以要告诉这个线程,其所要执行的代码是什么
System.out.println("hello thread!");
}
}
public class thread_1 {
//创建最基本线程的方法 : 创建子类,继承自 Thread
public static void main(String[] args) {
// 创建的 Thread 应为其子类,子类重写其 run 方法
Thread thread = new MyThread_1();
// 实例化 Thread 并执行 start 方法后,才真正创建了一个线程,并开始执行其 run 方法中的代码
thread.start();
}
}
②实现 Runnable 接口
//Runnable 就是在描述一个"任务"
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("hello Runnable!");
}
}
public class thread_2 {
//创建一个类,实现 Runnable 接口, 再创建 Runnable 实例传给 Thread 实例
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
③使用匿名内部类创建 Thread 子类对象
// 使用匿名内部类进行简化
public class thread_3 {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
System.out.println("hello thread_3!");
}
};
thread.start();
}
}
④使用匿名内部类创建 Runnable 子类对象
public class thread_4 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello thread_4!");
}
});
thread.start();
}
}
⑤使用lambda表达式
public class thread_5 {
public static void main(String[] args) {
Thread thread = new Thread(()->{
System.out.println("hello thread!");
});
thread.start();
}
}
2、线程休眠的方法:
调用Thread中的sleep()方法
class MyThread extends Thread{
@Override
public void run() {
while (true){
System.out.println("hello thread!");
// sleep 方法可强制让这个线程休眠一段时间
try {
// 让该线程在 1000 ms 内上不了 CPU
MyThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class thread {
public static void main(String[] args) {
Thread thread = new MyThread();
thread.start();
// 在 Java 进程中至少有一个 自动创建的调用 Main 方法的线程
// 此时这两个线程就是并发执行的
while (true){
System.out.println("hello Main!");
try {
MyThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3、线程等待
调用Thread中的join()方法
public class thread {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
//在主线程中,可以使用一个等待操作,来等待 thread 执行完毕
try {
//thread.join();
thread.join(1_0000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
4、线程中断
手动设置一个标志位来控制线程是否执行结束
public class thread_7 {
// 手动设置一个标志位来控制线程是否 执行结束
public static boolean isQuit = false;
public static void main(String[] args) {
Thread thread = new Thread(()->{
while (!isQuit){
System.out.println("hello thread!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
isQuit = true;
System.out.println("终止线程!");
}
}
Thread类内部包含了一个boolean的变量Thread.currentThread().isInterrupted()可以用来作为标记位,标记是否被中断。Thread中也包含着一个interrupt()方法,可以通过在主线程中,调用 interrupt ()方法,来中断这个线程。
public class thread{
public static void main(String[] args) {
Thread thread = new Thread( () -> {
// currentThread 方法: 获取当前线程的实例
// isInterrupted 方法: 判断对象关联的线程的标志位是否设置,调用后不清除标志位
while (!Thread.currentThread().isInterrupted()){
System.out.println("hello thread!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//触发异常后,立刻退出循环
break;
}
}
});
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在主线程中,调用 interrupt 方法,来中断这个线程
// 让 thread 线程被中断
thread.interrupt();
//可能产生的情况:
// 1. 如果 thread 线程处在就绪状态, 就是设置线程的标志位为 true
// 2. 如果 thread 线程处在阻塞状态, 就会触发一个 InterruptedException
}
}