一. 线程启动
线程启动 :
-- 1. 继承 Thread 运行线程 : 重写 Thread 类的 run 方法, 然后执行该线程;
-- 2. 实现 Runnable 接口, 并运行线程;
-- 代码示例 :
package com.hanshuliang.thread; public class ThreadStart { public static void main(String[] args) { //1. 继承 Thread 运行线程 MyThread thread = new MyThread(); thread.start(); //2. 实现 Runnable 接口, 并运行线程 Thread runnableThread = new Thread(new MyRunnable()); runnableThread.start(); } //1. 继承 Thread 类 static class MyThread extends Thread{ @Override public void run() { super.run(); System.out.println("MyThread 线程启动"); } } //2. 实现 Runnable 接口 static class MyRunnable implements Runnable{ @Override public void run() { System.out.println("MyRunnable 线程启动"); } } }
-- 运行结果 :
MyThread 线程启动
MyRunnable 线程启动
三. 线程停止
线程停止常用方法 :
-- 1. 使用 interrupt() 方法停止线程;
-- 2. 使用退出标志, 让线程正常退出;
-- 3. 弃用的方法 (不推荐) : 使用 stop() 方法强制停止线程, 但是该方法已经作废, 不建议使用;
1. 使用 interrupt() 方法停止线程
(1) 线程无法立即停止
interrupt() 使用说明 :
-- 打标记 : 调用该方法, 不能马上停止该线程, 只是在当前线程打了一个停止标记;
代码示例 :
-- 代码 :
public class InterruptDemo { public static class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 1; i <= 1000000; i++) //打印 一百万 数字, 大概持续 5 ~ 10 秒左右 System.out.println(i); } } public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(); thread.start(); //启动线程 Thread.sleep(100); //启动线程 100ms 后中断线程 thread.interrupt(); //中断线程 } }
-- 运行结果 : 这里只贴上最后几行 命令行的运行结果;
... ... 999996 999997 999998 999999 1000000
-- 总结 : 在上述程序中, 打印了 100 万数字, 从 1 到 100 0000, 整个过程持续了 10秒左右, 但是我们在 线程开始后 100ms 就中断了线程, 但是线程还是执行完毕了, 说明线程并没有在调用 interrupt() 方法后立即停止;
(2) 线程停止状态判定
两个线程停止状态判定的方法 :
-- 1. interrupted() 方法 : ①判断当前线程的中断标志, ②如果是中断标志 true, 那么清除中断标志, 改为 false;,③ 连续两次调用该方法, 第二次返回 false, ④ 静态方法 : 该方法是测试当前线程的中断标志, 在哪个线程中调用, 就是判定的哪个线程的中断标志, 不管调用的主体是哪个线程;
-- 2. isInterrupted() 方法 : 判断线程的 中断状态, 不管真实的运行状态, 只关心状态;
-- 注意 : 两个方法都是判断 中断状态的, 与线程的真实运行状况无关;
(3) interrupted() 方法测试
interrupted () 方法测试1 : 测试 interrupted 方法的判断是否已经中断的效果;
-- 测试代码 :
public class InterruptedDemo1 { public static class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 1; i <= 10; i++) //打印10个数字 System.out.println(i); } } public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(); thread.start(); //启动线程 thread.interrupt(); //启动线程后马上 中断线程 System.out.println("第一次 : thread.interrupted() = " + thread.interrupted()); System.out.println("第二次 : thread.interrupted() = " + thread.interrupted()); } }
-- 执行结果 :
第一次 : thread.interrupted() = false 1 2 第二次 : thread.interrupted() = false 3 4 5 6 7 8 9 10
-- 总结 : 启动线程后, 立即调用 interrupt 方法 中断线程, 但是 在主线程中 调用 thread.Interrupted() 方法, 打印出来的是 主线程的中断状态标志, 虽然是调用的 thread 子线程的对象的方法, 但是该方法是一个静态方法, 在哪个线程调用, 就是打印哪个线程的中断标志;
interrupted () 方法测试2 : 测试 interrupted 方法的 清除 中断状态 的效果;
-- 1. 测试代码 :
public class InterruptedDemo { public static void main(String[] args) throws InterruptedException { Thread.currentThread().interrupt(); //中断主线程 System.out.println("第一次 : thread.interrupted() = " + Thread.interrupted()); System.out.println("第二次 : thread.interrupted() = " + Thread.interrupted()); } }
-- 2. 执行结果 :
第一次 : thread.interrupted() = true 第二次 : thread.interrupted() = false
-- 3. 总结 : 使用 interrupted() 方法, 如果当前线程的状态是中断状态, 即返回了 true, 那么需要清除该标志, 下一次调用 interrupted() 方法后, 返回值就是 false 了;
(4) isInterrupted() 方法测试
isInterrupted() 方法测试1 : 测试其 中断状态, 与上面的 interrupted() 方法对比;
-- 1. 测试代码 :
public class IsInterruptedDemo { public static class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 1; i <= 10; i++) //打印10个数字 System.out.println(i); } } public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(); thread.start(); //启动线程 thread.interrupt(); //启动线程后马上 中断线程 System.out.println("第一次 : thread.interrupted() = " + thread.isInterrupted()); System.out.println("第二次 : thread.interrupted() = " + thread.isInterrupted()); } }
-- 2. 返回值 :
第一次 : thread.interrupted() = true 1 2 第二次 : thread.interrupted() = true 3 4 5 6 7 8 9 10
-- 3. 总结分析 : isInterrupted() 方法 只 判断 被调用对象的 该对象线程的 线程的中断 状态, 不管线程的真实运行状况, 即使当前线程正在运行, 但是线程调用了 interrupt() 方法, 其中断状态为 true, 因此 isInterrupted() 方法的返回值一直是 true;
-- 4. 对比 interrupted() 方法 : interrupted() 方法反应的是真实的线程运行状态, 线程正在运行, 那么返回 false, 如果线程没有运行, 返回 true;
-- 5. 对比 Interrupted() 方法 (静态与普通方法) : isInterrupted 方法是非静态方法, 哪个对象调用返回的就是哪个对象的中断状态; interrupted 是静态方法, 在哪个线程调用就是返回哪个线程的中断状态;