本篇文章给大家分享的是有关Java中实现线程的方式有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Java中实现多线程的方式的方式中最核心的就是 run()
方法,不管何种方式其最终都是通过run()
来运行。
Java刚发布时也就是JDK 1.0版本提供了两种实现方式,一个是继承Thread
类,一个是实现Runnable
接口。两种方式都是去重写run()
方法,在run()
方法中去实现具体的业务代码。
但这两种方式有一个共同的弊端,就是由于run()
方法是没有返回值的,所以通过这两方式实现的多线程读无法获得执行的结果。
为了解决这个问题在JDK 1.5的时候引入一个Callable<V>
接口,根据泛型V
设定返回值的类型,实现他的call()
方法,可以获得线程执行的返回结果。
虽然call()
方法可以获得返回值,但它需要配合一个Future<V>
才能拿到返回结果,而这个Future<V>
又是继承了Runnable
的一个接口。通过查阅源码就可以发现Future<V>
的实现FutureTask<V>
其在做具体业务代码执行的时候仍是在run()
里面实现的。
public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { Callable<V> c = callable; if (c != null && state == NEW) { V result; boolean ran; try { result = c.call(); ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) set(result); } } finally { // runner must be non-null until state is settled to // prevent concurrent calls to run() runner = null; // state must be re-read after nulling runner to prevent // leaked interrupts int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } }
public class ThreadTest { public static void main(String[] args) throws Exception { Thread myThread = new MyThread(); myThread.setName("MyThread-entends-Thread-test"); myThread.start(); }}class MyThread extends Thread { @Override public void run() { System.out.println("Thread Name:" + Thread.currentThread().getName()); }}
public class ThreadTest { public static void main(String[] args) throws Exception { MyRunnableThread myRunnable = new MyRunnableThread(); Thread myRunnableThread = new Thread(myRunnable); myRunnableThread.setName("MyThread-implements-Runnable-test"); myRunnableThread.start(); }}class MyRunnableThread implements Runnable { @Override public void run() { System.out.println("Thread Name:" + Thread.currentThread().getName()); }}
public class ThreadTest { public static void main(String[] args) throws Exception { Callable<String> myCallable = new MyCallableThread(); FutureTask<String> futureTask = new FutureTask<>(myCallable); Thread myCallableThread = new Thread(futureTask); myCallableThread.setName("MyThread-implements-Callable-test"); myCallableThread.start(); System.out.println("Run by Thread:" + futureTask.get()); //通过线程池执行 ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(futureTask); executorService.shutdown(); System.out.println("Run by ExecutorService:" + futureTask.get()); }}class MyCallableThread implements Callable<String> { @Override public String call() throws Exception { return Thread.currentThread().getName(); }}
以上就是Java中实现线程的方式有哪些,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/codingdiary/blog/4358557