一.线程创建第一方式创建子类继承Thread 重写run方法package Thread_star; public class Dome1 extends Thread { Override public void run() { while (true){ System.out.println(hello thread); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void main(String[] args) throws InterruptedException { Thread thread new Dome1(); thread.start(); while (true){ System.out.println(hello main); thread.sleep(1_000); } } }第二方式继承Runnable这个接口 重写run方法package Thread_star; public class Dome2 implements Runnable{ Override public void run() { while (true){ System.out.println(hello thread); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void main(String[] args) throws InterruptedException { Dome2 dome2 new Dome2(); Thread thread new Thread(dome2); thread.start(); while (true){ System.out.println(hello main); thread.sleep(1_000); } } }第三方式直接创建Thread 的子类 匿名内部类重写run方法package Thread_star; public class Dome3 { public static void main(String[] args) throws InterruptedException { Thread thread new Thread(){ Override public void run() { while (true){ System.out.println(hello thread); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; thread.start(); while (true){ System.out.println(hello main); thread.sleep(1_000); } } }第四方式直接创建Runnable的子类 匿名内部类重写run方法package Thread_star; public class Dome4 { public static void main(String[] args) throws InterruptedException { Runnable runnable new Runnable() { Override public void run() { while (true){ System.out.println(hello thread); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; Thread thread new Thread(runnable); thread.start(); while (true){ System.out.println(hello main); thread.sleep(1_000); } } }第五方式直接创建Thread 的子类 Lambda表达式 重写run方法package Thread_star; public class Dome5 { public static void main(String[] args) throws InterruptedException { Thread thread new Thread(() - {while (true){ System.out.println(hello thread); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }, 尝试1); thread.start(); while (true){ System.out.println(hello main); thread.sleep(1_000); } } }二.线程中断 获取线程实例 线程等待 线程休眠package Thread_star; import java.util.Scanner; public class Dome6 { public static int count 0; public static void main(String[] args) throws InterruptedException { Thread thread new Thread(() - { //线程的获取实力方式 while (!Thread.currentThread().isInterrupted()){ System.out.println(线程中断); try { //线程的休眠 sleep方法 Thread.sleep(10_000); } catch (InterruptedException e) { break; } } },线程中断); //《--这个位置给线起名字 //启动线程 thread.start(); //获取线程的名字 System.out.println(thread.getName()); for (int i 0; i 10; i) { count; } System.out.println(count); Scanner scanner new Scanner(System.in); System.out.println(输入0 中断线程); int n scanner.nextInt(); if (n 0){ thread.interrupt(); } thread.join(); } }1.线程中断1.方式是在线程里面用2先用currentThread()方法返回当前代码所在线程的对象3再用isInterrupted()方法返回一个 boolean 值读取线程内部的「中断标志位」4下面的interrupt()方法用于改变 isInterrupted 中的值isInterrupted 默认值为 false。2.线程等待(1).join()当前线程调用者等待 x 线程执行完毕后再继续往下走。上述代码就是等待thread运行完之后再去运行maim这个主线程3.线程休眠1.sleep(毫秒)让「当前线程」暂停指定时间交出 CPU。 等完后在去运行。4.获取线程实例1Thread.currentThread()拿到「当前代码所在」的线程对象完成的流程【准备阶段 - main 线程】1. new Thread(...) 创建子线程对象起名线程中断此时未启动2. thread.start() 启动子线程main 与子线程开始【并发】执行【并发阶段】main 线程做什么a. thread.getName() 打印线程名 → 输出线程中断b. for 循环 count 十次 → count 10c. println(count) → 输出 10d. 创建 Scanner打印输入0 中断线程e. scanner.nextInt() → main 阻塞等待你输入子线程做什么a. 判断 !isInterrupted() 为 true → 进入循环b. 打印线程中断c. Thread.sleep(10000) → 睡着 10 秒【中断阶段】当你输入 0 并回车3. n 0 成立 → 调用 thread.interrupt()4. interrupt() 做两件事- 把子线程的中断标志位设为 true- 唤醒正在 sleep 的子线程抛出 InterruptedException5. 子线程 catch 到异常 → break → 跳出循环 → 子线程结束【等待结束阶段】6. thread.join() → main 等子线程结束此时子线程已退出join 立刻通过7. main 线程结束程序退出