先看一段代码
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.FutureTask;
- class ThreadA extends Thread
- {
- @Override
- public void run() {
- System.out.println("继承自 Thread 类的 ThreadA");
- }
- }
- class ThreadB implements Runnable
- {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("实现 Runnable 接口的 ThreadB");
- }
- }
- class ThreadC implements Callable<String>
- {
- @Override
- public String call() throws Exception {
- // TODO Auto-generated method stub
- return "实现类 Callable 接口的 ThreadC";
- }
- }
- public class CreateThread {
- public static void main(String[] args) {
- new Thread(new ThreadA()).start();
- new Thread(new ThreadB()).start();
- ThreadC threadC = new ThreadC();
- FutureTask<String> futureTask = new FutureTask<String>(threadC) ;
- new Thread(futureTask).start();
- try {
- System.out.println(futureTask.get());
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ExecutionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
待编辑
来源: http://www.bubuko.com/infodetail-2974305.html