Java多线程并发01——线程的创建与终止,你会几种方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java多线程并发01——线程的创建与终止,你会几种方式

耒耒耒耒耒   2020-03-15 我要评论
> 本文开始将开始介绍 Java 多线程与并发相关的知识,多谢各位一直以来的关注与支持。关注我的公众号「Java面典」了解更多 Java 相关知识点。 # 线程的创建方式 在 Java 中,用户常用的主动创建线程的方式有三种,分别是 **继承 Thread 类**、**实现 Runnable 接口** 、**通过Callable 和 Future**。 ## 继承 Thread 类 * 定义 Thread 类的子类,并重写该类的 run 方法; * 调用线程对象的 start() 方法来启动该线程。 **通过继承 Thread 实现的线程类,多个线程间无法共享线程类的实例变量(需要创建不同 Thread 对象)。** ```java /** * 通过继承Thread实现线程 */ public class MyThread extends Thread { public void run() { System.out.println("MyThread.run()"); } } MyThread myThread = new MyThread(); myThread.start(); ``` ## 实现 Runnable 接口 * 如果自己的类已经 extends 另一个类,就无法直接 extends Thread,此时,可以实现一个 Runnable 接口; * 调用线程对象的start()方法来启动该线程。 ```java /** * 通过实现Runnable接口实现的线程类 */ public class RunnableTest implements Runnable { @Override public void run() { System.out.println("RunnableTest.run()"); } public static void main(String[] args) { RunnableTest runnableTest = new RunnableTest() ; Thread thread = new Thread(runnableTest); thread.start(); } } ``` ## 通过 Callable、Future 从 Thread 和 Runnable 两种方式可以看出,两种方式都**不支持返回值**,且**不能声明抛出异常**。 而 Callable 接口则实现了此两点,Callable 接口如同 Runable 接口的升级版,其提供的 call() 方法将作为线程的执行体,同时允许有返回值。 但是 Callable 对象不能直接作为 Thread 对象的 target,我们可以使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值。 ```java import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class CallableTest { public static void main(String[] args) { CallableTest callableTest = new CallableTest() ; //因为Callable接口是函数式接口,可以使用Lambda表达式 FutureTask task = new FutureTask((Callable)()->{ System.out.println("FutureTask and Callable"); return "hello word"; }); try{ System.out.println("子线程返回值 : " + task.get()); } catch (Exception e){ e.printStackTrace(); } } } ``` # 线程的终止方式 线程除了正常结束外,还可以通过特定方式终止线程,终止线程常用的方式有以下三种:**使用退出标志退出线程**、** Interrupt 方法结束线程**、**stop 方法终止线程**。 ## 使用退出标志退出线程 最常使用的方式其实现方式是:定义一个 boolean 型的标志位,在线程的 run() 方法中根据这个标志位是 true 还是 false 来判断是否退出,这种情况一般是将任务放在 run() 方法中的一个 while 循环中执行的。 ```java public class ThreadSafe extends Thread { public volatile boolean exit = false; public void run() { while (!exit){ /https://img.qb5200.com/download-x/do work } } public static void main(String[] args) throws Exception { ThreadFlag thread = new ThreadFlag(); thread.start(); sleep(5000); // 主线程延迟5秒 thread.exit = true; // 终止线程thread thread.join(); System.out.println("线程退出!"); } } ``` ## Interrupt 方法结束线程 使用 interrupt() 方法来中断线程有两种情况: 1. **线程处于阻塞状态**。如使用了 sleep,同步锁的 wait,socket 中的 receiver,accept 等方法时,会使线程处于阻塞状态。`使用 interrupt 方法结束线程的时候,一定要先捕获 InterruptedException 异常之后通过 break 来跳出循环,才能正常结束 run 方法。` ```java public class ThreadInterrupt extends Thread { public void run() { try { sleep(50000); // 延迟50秒 } catch (InterruptedException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) throws Exception { Thread thread = new ThreadInterrupt(); thread.start(); System.out.println("在50秒之内按任意键中断线程!"); System.in.read(); thread.interrupt(); thread.join(); System.out.println("线程已经退出!"); } } ``` 2. **线程未处于阻塞状态**。使用 isInterrupted() 判断线程的中断标志来退出循环。当使用 interrupt() 方法时,中断标志就会置 true,和使用自定义的标志来控制循环是一样的道理。 ```java public class ThreadSafe extends Thread { public void run() { while (!isInterrupted()) { //非阻塞过程中通过判断中断标志来退出 try { Thread.sleep(5*1000);//阻塞过程捕获中断异常来退出 } catch (InterruptedException e) { e.printStackTrace(); break;//捕获到异常之后,执行 break 跳出循环 } } } } ``` ## stop 方法终止线程 使用 stop 方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程: ```java thread.stop(); ``` **采用 stop 是不安全的**,主要影响点如下: 1. thread.stop() 调用之后,创建子线程的线程就会抛出 ThreadDeatherror 的错误; 2. 调用 stop 会释放子线程所持有的所有锁。导致了该线程所持有的所有锁的突然释放(不可控制),那么被保护数据就有可能呈现不一致性。 # 总结 * **线程创建**:推荐使用 Runnable 或者 Callable 方式创建线程,相比继承,接口实现可以更加灵活,不会受限于Java的单继承机制。 * **线程终止**:线程终止推荐使用 标志位 或 Interrupt 方式终止,stop 方式对线程不安全,易导致数据不一致。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们