我们已经知道创建线程的方式有1.继承thread类。2.实现Runnable接口
接下来讲创建线程的新方式Callable接口,首先对比一下Runnable接口和Callable接口的区别:
首先创建两个资源类:分别是实现了Runnable接口和实现了Callable接口:
//Runnable接口 class MyThreadRunnable implements Runnable { @Override public void run() { } } //Callable class MyThreadCallable implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("******come in here"); return 1024; } }
我们可以看到Callable存在泛型,以及返回值,这是对原来的老技术的增强,因为存在了返回值,提高了线程的细粒度。
接着我们看看Runnable创建线程的方式:
//Runnable MyThreadRunnable myThread1=new MyThreadRunnable(); Thread t1=new Thread(myThread1);
但是通过该方式我们利用Callable来创建线程,却报错了,这是为什么 呢?
原因:Thread并不存在Callable的构造器!
如何创建Callable线程
首先查看API,看Runable接口:
过程如下:
我们可以看到的是,这个构造器需要的参数就是Callable接口的实现类。
所以,我们创建线程的方式如下:
public class CallableDemo { public static void main(String[] args) { // MyThreadCallable myThread = new MyThreadCallable(); FutureTask futureTask = new FutureTask(new MyThreadCallable()); new Thread(futureTask, "A").start(); System.out.println(futureTask.get());// 1024 通过get方法来获取返回值 } }
get方法具有阻塞性
public class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { // MyThreadCallable myThread = new MyThreadCallable(); FutureTask futureTask = new FutureTask(new MyThreadCallable()); new Thread(futureTask, "A").start(); System.out.println(futureTask.get());// 1024 通过get方式来获取返回值 该方法会阻塞! System.out.println(Thread.currentThread().getName()+"***计算完成"); } } //Callable class MyThreadCallable implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("******come in here"); Thread.sleep(5000); return 1024; } }
然后调转依一下主线程与futureTask线程执行的顺序:
public class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { // MyThreadCallable myThread = new MyThreadCallable(); FutureTask futureTask = new FutureTask(new MyThreadCallable()); new Thread(futureTask, "A").start(); System.out.println(Thread.currentThread().getName()+"***计算完成"); System.out.println(futureTask.get());// 1024 通过get方式来获取返回值 该方法会阻塞! } } //Callable class MyThreadCallable implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("******come in here"); Thread.sleep(5000); return 1024; } }
往往futureTask里面的get方法会被阻塞, 所以一般情况下我们先让main线程执行完毕防止由于等待futureTask而耗时。
futureTask的单一性
新增一个线程B:
public class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { // MyThreadCallable myThread = new MyThreadCallable(); FutureTask futureTask = new FutureTask(new MyThreadCallable()); new Thread(futureTask, "A").start(); new Thread(futureTask, "B").start(); System.out.println(Thread.currentThread().getName() + "***计算完成"); System.out.println(futureTask.get());// 1024 通过get方式来获取返回值 该方法会阻塞! } } //Callable class MyThreadCallable implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("******come in here"); Thread.sleep(5000); return 1024; } }
只执行了一次,因为一个futureTask,不管几个线程调用,调用的都是同一个futureTask对象!而且Runnable接口就不一样了:
public class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { MyThreadRunnable t = new MyThreadRunnable(); Thread thread = new Thread(t); new Thread(thread).run(); new Thread(thread).run(); } } //Runnable接口 class MyThreadRunnable implements Runnable { @Override public void run() { System.out.println("******come in here"); } }
以上..