Java中的Callable和Future创建新线程
有两种创建新线程的方法:1. extend Thread; 2 implement Runnable接口。Runnable接口的一个缺点是当线程终止(主线程调用run()结束 ), 不能得到任何返回值。为了实现这种功能,Java 提供了 Callable接口。
下面是例子。
// Java program to illustrate Callable and FutureTask
// for random number generation
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class CallableExample implements Callable
{
public Object call() throws Exception
{
Random generator = new Random();
Integer randomNumber = generator.nextInt