ShineYoung

导航

 

先看一段代码

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();
        }
    }
}

待编辑

posted on 2019-03-03 09:46  ShineYoung  阅读(125)  评论(0编辑  收藏  举报