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方法来获取返回值
}
}
细节
使用
callable
就相当于
另外
开了一条线程运行,调用
get
方法就相当于要获取这条线程的运行结
果。
如果在
mian
线程中调用了
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;
}
}
新增一个线程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");
}
}