我们自然知道创建线程的三种方式,前两种就不多说了,这次我们讲的是第三种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
MyThreadRunnable myThread1=new MyThreadRunnable();
Thread t1=new Thread(myThread1);
但是通过该方式我们利用Callable来创建线程,却报错了,这是为什么 呢?
原因:Thread并不存在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方法来获取返回值
}
}
这里我们看一些细节:
细节一:
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;
}
}
然后调转依一下位置
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;
}
}
所以我们可以知道,get方法具有阻塞的效果。
细节二:
新增一个线程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");
}
}
以上就是通过实现Callable接口来创建线程的方式。