本文首发于本博客 猫叔的博客,转载请申明出处

前言

并发是一件很美妙的事情,线程的调度与使用会让你除了业务代码外,有新的世界观,无论你是否参与但是这对于你未来的成长帮助很大。

所以,让我们来好好看看在Java中启动线程的那几个方式与介绍。

Thread

对于 Thread 我想这个基本上大家都认识的,在Java源码是这样说: java 虚拟机允许应用程序同时运行多个执行线程。 而这个的 Thread 就是程序的执行线程。

如何使用它呢,其实在这个类中的源码已经给我们写好了,甚至是下面的 Runnable 的使用方式。(如下是Thread源码)

/** * A <i>thread</i> is a thread of execution in a program. The Java * Virtual Machine allows an application to have multiple threads of * execution running concurrently. * <hr><blockquote><pre> * class PrimeThread extends Thread { * long minPrime; * PrimeThread(long minPrime) { * this.minPrime = minPrime; * } * * public void run() { * // compute primes larger than minPrime * &nbsp;.&nbsp;.&nbsp;. * } * } * </pre></blockquote><hr> * <p> * The following code would then create a thread and start it running: * <blockquote><pre> * PrimeThread p = new PrimeThread(143); * p.start(); * </pre></blockquote> * <p> * <hr><blockquote><pre> * class PrimeRun implements Runnable { * long minPrime; * PrimeRun(long minPrime) { * this.minPrime = minPrime; * } * * public void run() { * // compute primes larger than minPrime * &nbsp;.&nbsp;.&nbsp;. * } * } * </pre></blockquote><hr> * <p> * The following code would then create a thread and start it running: * <blockquote><pre> * PrimeRun p = new PrimeRun(143); * new Thread(p).start(); * </pre></blockquote> * <p> */
public class Thread implements Runnable {
//...
}

阅读源码的信息其实是最全的 ,我截取了部分的注释信息,起码我们现在可以无压力的使用这个两个方式来启动自己的线程。

如果我们还要传递参数的话,那么我们设定一个自己的构造函数也是可以,如下方式:

public class MyThread extends Thread {

    public MyThread(String name) {
super(name);
} @Override
public void run() {
System.out.println("一个子线程 BY " + getName());
}
}

这时读者应该发现,这个构造函数中的 name ,居然在 Thread 中也是有的,其实在Java中的线程都会自己的名称,如果我们不给其定义名称的话,java也会自己给其命名。

/** * Allocates a new {@code Thread} object. This constructor has the same * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} * {@code (null, null, name)}. * * @param name * the name of the new thread */
public Thread(String name) {
init(null, null, name, 0);
}

而我们最核心,也是大家最在意的应该就是如何启动并执行我们的线程了,是的,这个大家都知道的,就是这个 run 方法了。

同时大家如果了解过了 Runnable ,我想大家都会知道这个 run 方法,其实是 Runnable 的方法,而我们本节的 Thread 也是实现了这个接口。

这里,大家可能会好奇,不是应该是 start 这个方法吗?那么让我们看看 start 的源码。

/** * Causes this thread to begin execution; the Java Virtual Machin * calls the <code>run</code> method of this thread. */
public synchronized void start() {
//...
}

通过 start 方法,我们可以了解到,就如同源码的启动模板中那样,官网希望,对于线程的启动,使用者是通过 start 的方式来启动线程,因为这个方法会让Java虚拟机会调用这个线程的 run 方法。

其结果就是,一个线程去运行 start 方法,而另一个线程则取运行 run 方法。同时对于这样线程,Java官方也说了,线程是不允许多次启动的,这是不合法的。

所以如果我们执行下面的代码,就会报 java.lang.IllegalThreadStateException 异常。

MyThread myThread = new MyThread("Thread");
myThread.start();
myThread.start();

但是,如果是这样的代码呢?

MyThread myThread = new MyThread("Thread");
myThread.run();
myThread.run();
myThread.start(); //运行结果
一个子线程 BY Thread
一个子线程 BY Thread
一个子线程 BY Thread

这是不合理的,如果大家有兴趣,可以去试试并动手测试下,最好开调试模式。

下面我们再看看,连 Thread 都要实现,且核心的 run 方法出处的 Runnable 。

Runnable

比起 Thread 我希望大家跟多的使用 Runnable 这个接口实现的方式,对于好坏对比会在总结篇说下。

我想大家看 Runnable 的源码会更加容易与容易接受,毕竟它有一个 run 方法。(如下为其源码)

/** * The <code>Runnable</code> interface should be implemented by any * class whose instances are intended to be executed by a thread. The * class must define a method of no arguments called <code>run</code>. */
@FunctionalInterface
public interface Runnable {
/** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. */
public abstract void run();
}

首先,所有打算执行线程的类均可实现这个 Runnable 接口,且必须实现 run 方法。

它将为各个类提供一个协议,就像 Thread 一样,其实当我们的类实现了 Runnable 的接口后,我们的类与 Thread 是同级,只是可能仅有 run 方法,而没有 Thread 提供的跟丰富的功能方法。

而对于 run 方法,则是所有实现了 Runnable 接口的类,在调用 start 后,将使其单独执行 run 方法。

那么我们可以写出这样的测试代码。

MyThreadRunnable myThreadRunnable = new MyThreadRunnable("Runnabel");
myThreadRunnable.run();
new Thread(myThreadRunnable).start();
Thread thread = new Thread(myThreadRunnable);
thread.start();
thread.start(); //运行效果
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at com.github.myself.runner.RunnableApplication.main(RunnableApplication.java:14)
这是一个子线程 BY Runnabel
这是一个子线程 BY Runnabel
这是一个子线程 BY Runnabel

同样的,线程是不允许多次启动的,这是不合法的。

同时,这时我们也看出了使用 Thread 与 Runnable 的区别,当我们要多次启用一个相同的功能时。

我想 Runnable 更适合你。

但是,用了这两个方式,我们要如何知道线程的运行结果呢???

FutureTask

这个可能很少人(初学者)用到,不过这个现在是我最感兴趣的。它很有趣。

其实还有一个小兄弟,那就是 Callable。 它们是一对搭档。如果上面的内容,你已经细细品味过,那么你应该已经发现 Callable 了。

没错,他就在 Runnable 的源码中出现过。

/** * @author Arthur van Hoff * @see java.lang.Thread * @see java.util.concurrent.Callable * @since JDK1.0 */
@FunctionalInterface
public interface Runnable {}

那么我们先去看看这个 Callable 吧。(如下为其源码)

/** * A task that returns a result and may throw an exception. * Implementors define a single method with no arguments called * {@code call}. */
@FunctionalInterface
public interface Callable<V> {
/** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */
V call() throws Exception;
}

其实,这是一个与 Runnable 基本相同的接口,当时它可以返回执行结果与检查异常,其计算结果将由 call() 方法返回。

那么其实我们现在可以写出一个实现的类。

public class MyCallable implements Callable {

    private String name;

    public MyCallable(String name) {
this.name = name;
} @Override
public Object call() throws Exception {
System.out.println("这是一个子线程 BY " + name);
return "successs";
}
}

关于更深入的探讨,我将留到下一篇文章中。

好了,我想我们应该来看看 FutureTask 这个类的相关信息了。

/** * A cancellable asynchronous computation. This class provides a base * implementation of {@link Future}, with methods to start and cancel * a computation, query to see if the computation is complete, and * retrieve the result of the computation. The result can only be * retrieved when the computation has completed; the {@code get} * methods will block if the computation has not yet completed. Once * the computation has completed, the computation cannot be restarted * or cancelled (unless the computation is invoked using * {@link #runAndReset}). */
public class FutureTask<V> implements RunnableFuture<V> {
//...
}

源码写的很清楚,这是一个可以取消的异步计算,提供了查询、计算、查看结果等的方法,同时我们还可以使用 runAndRest 来让我们可以重新启动计算。

在查看其构造函数的时候,很高兴,我们看到了我们的 Callable 接口。

/** * Creates a {@code FutureTask} that will, upon running, execute the * given {@code Callable}. * * @param callable the callable task * @throws NullPointerException if the callable is null */
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}

即我们将创建一个未来任务,来执行 Callable 的实现类。那么我们现在可以写出这样的代码了。

final FutureTask fun = new FutureTask(new MyCallable("Future"));

那么接下来我们就可以运行我们的任务了吗?

是的,我知道了 run() 方法,但是却没有 start 方法。

官方既然说有结果,那么我找到了 get 方法。同时我尝试着写了一下测试代码。

public static void main(String[] args) {
MyCallable myCallable = new MyCallable("Callable");
final FutureTask fun = new FutureTask(myCallable);
fun.run();
try {
Object result = fun.get();
System.out.println(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}

运行效果,是正常的,这好像是那么回事。

//运行效果
这是一个子线程 BY Callable
successs

可是,在我尝试着加多一些代码的时候,却发现了一些奇妙的东西 。

我加多了一行 fun.run(); 代码,同时在 MyCallable 类中,将方法加一个时间线程去等待3s。

结果是: 结果只输出了一次,同时 get 方法需要等运行3s后才有返回。

这并不是我希望看到的。但是,起码我们可以知道,这次即使我们多次运行使用 run 方法,但是这个线程也只运行了一次。这是一个好消息。

同时,我们也拿到了任务的结果,当时我们的进程被阻塞了,我们需要去等我们的任务执行完成。

最后,在一番小研究后,以下的代码终于完成了我们预期的期望。

public static void main(String[] args) {
MyCallable myCallable = new MyCallable("Callable");
ExecutorService executorService = Executors.newCachedThreadPool();
final FutureTask fun = new FutureTask(myCallable);
executorService.execute(fun);
// fun.run(); //阻塞进程
System.out.println("--继续执行");
try {
Object result = fun.get();
System.out.println(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}

我们使用线程池去运行我们的 FutureTask 同时使用 get 方法去获取运行后的结果。结果是友好的,进程并不会被阻塞。

关于更深入的探讨,我将留到下一篇文章中。

总结一波

好了,现在应该来整理以下了。

  • Thread 需要我们继承实现,这是比较局限的,因为Java的 继承资源 是有限的,同时如果多次执行任务,还需要 多次创建任务类
  • Runnable 以接口的形式让我们实现,较为方便,同时多次执行任务也无需创建多个任务类,当时仅有一个 run 方法。
  • 以上两个方法都 无法获取任务执行结果 ,FutureTask可以获取任务结果。同时还有更多的新特性方便我们使用···

公众号:Java猫说

现架构设计(码农)兼创业技术顾问,不羁平庸,热爱开源,杂谈程序人生与不定期干货。


简说Java线程的那几个启动方式的更多相关文章

  1. Java线程间通信-回调的实现方式

    Java线程间通信-回调的实现方式   Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互.   比如举一个简单例子,有一个多线程的 ...

  2. java线程基础巩固---创建并启动线程

    对于java的并发编程方面的东东,不管是面试还是实际工作开发都是非常重要的,而往往只要涉及到并发相关的东东有点让人觉得有点难,而实际工作中涉及到并发可能就是简单的用下同步块.上锁之类的一些简单的操作, ...

  3. java基础知识回顾之java Thread类学习(三)--java线程实现常见的两种方式实现好处:

    总结:实现Runnable接口比继承Thread类更有优势: 1.因为java只能单继承,实现Runnable接口可以避免单继承的局限性 2.继承Thread类,多个线程不能处理或者共享同一个资源,但 ...

  4. java线程阻塞唤醒的四种方式

    java在多线程情况下,经常会使用到线程的阻塞与唤醒,这里就为大家简单介绍一下以下几种阻塞/唤醒方式与区别,不做详细的介绍与代码分析 suspend与resume Java废弃 suspend() 去 ...

  5. Java线程实现的第三种方式Callable方式与结合Future获取返回值

    多线程的实现方式有实现Runnable接口和继承Thread类(实际上Thread类也实现了Runnable接口),但是Runnable接口的方式有两个弊端,第一个是不能获取返回结果,第二个是不能抛出 ...

  6. Java线程池的四种创建方式

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. newFix ...

  7. java基础知识回顾之java Thread类--java线程实现常见的两种方式实现Runnable接口(二)

    创建线程的第二中方式: /** *      步骤: 1定义类实现Runnable接口      2.实现Runnable接口中的run方法.      3.通过Thread类建立线程对象,并将Run ...

  8. 获取Java线程返回值的几种方式

    在实际开发过程中,我们有时候会遇到主线程调用子线程,要等待子线程返回的结果来进行下一步动作的业务. 那么怎么获取子线程返回的值呢,我这里总结了三种方式: 主线程等待. Join方法等待. 实现Call ...

  9. Java线程和进程

    一.线程 1.什么是线程: 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.一个进程至少包含一个线程,也可以多个,线程属于进程. 2.Java中线程经历的四个 ...

随机推荐

  1. go语言调度器源代码情景分析之三:内存

    本文是<go调度器源代码情景分析>系列 第一章 预备知识的第2小节. 内存是计算机系统的存储设备,其主要作用是协助CPU在执行程序时存储数据和指令. 内存由大量内存单元组成,内存单元大小为 ...

  2. 这年头做开源项目,被冷嘲热讽,FreeSql 0.0.4

    FreeSql 项目大概在20天前想着要做的,今天发布0.0.4在群里被一位大神讽刺. 这位无名氏哥们的观点,先声明这不是找安慰的文章,更加不是报复打击的目的. 1 所以这个比EF好在哪里 2 毕竟E ...

  3. Maven构建Struts2项目

    1.添加Struts2依赖 这里主需要在pom.xml中添加一个struts-core的依赖即可: <project xmlns="http://maven.apache.org/PO ...

  4. 入门者必看!SharePoint之CAML总结(实战)

    分享人:广州华软 无名 一. 前言 在SharePoint中,不支持直接操作数据库,但开发过程中,避免不了查询数据,那么,在SharePoint中如何查询数据? 当然是使用CAML语法. 二. 目录 ...

  5. 一次apk打开时报内存溢出错误,故写下内存溢出的各种原因和解决方法

    原转载:https://blog.csdn.net/cp_panda_5/article/details/79613870 正文内容: 对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么 ...

  6. gitbook 入门教程之使用 gitbook-editor 编辑器开发电子书

    亲测,目前已不再支持旧版 gitbook-editor 编辑器,而官网也没有相应的新版编辑器,如果哪位找到了新版编辑器,还望告知! 现在注册 gitbook 账号会默认重定向到 新版官网,而 旧版官网 ...

  7. Django【部署】uwsgi+nginx

    uwsgi 遵循wsgi协议的web服务器 uwsgi的安装 pip install uwsgi uwsgi的配置 项目部署时,需要把settings.py文件夹下的: DEBUG = FALSE A ...

  8. JournalNode failed to restart

    Install clusterEnable Namenode HAStart RU"Zookeeper" is completed"Core Masters" ...

  9. ASP.NET Core 共享第三方依赖库部署的正常打开方式

    曾经: 写了一篇: ASP.Net Core on Linux (CentOS7) 共享第三方依赖库部署 当第二次想做相同的事,却遇上了Bug,于是有了第二篇: ASP.NET Core 共享第三方依 ...

  10. Python算法练习--把搜索树转成双向链表

    本文目前分享的题目都是来自于July的分享,然后把具体算法实现.搜索树转双向链表主要的实现逻辑是在中序遍历时,调整节点的左右子树:因为中序遍历是递归调用,所以在调整时一定要注意调整的位置,如果写错了, ...