FutureTask用法

这里我们先举个例子

Runnable

public class RunnableDemo implements Runnable{

    @Override
    public void run() {
        System.out.printf("子线程 %s 开始: %s \n", Thread.currentThread().getName(),getDate());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("子线程 %s 结束: %s \n", Thread.currentThread().getName(),getDate());
    }

    public static String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        return  dateFormat.format(date);
    }

    public static void main(String []args){
        System.out.printf("主线程 %s 开始: %s \n", Thread.currentThread().getName(),getDate());
        RunnableDemo runnableDemo = new RunnableDemo();
        Thread thread = new Thread(runnableDemo);
        thread.start();
        System.out.printf("主线程 %s 结束: %s \n", Thread.currentThread().getName(),getDate());
    }
}

结果如下

主线程 main 开始: 2019-05-25 :11:06:46 
主线程 main 结束: 2019-05-25 :11:06:46 
子线程 Thread-0 开始: 2019-05-25 :11:06:46 
子线程 Thread-0 结束: 2019-05-25 :11:06:51 

FutureTask

public class FutureTaskDemo implements Runnable{

    @Override
    public void run() {
        System.out.printf("子线程 %s 开始: %s \n", Thread.currentThread().getName(),getDate());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("子线程 %s 结束: %s \n", Thread.currentThread().getName(),getDate());
    }

    public static String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        return  dateFormat.format(date);
    }

    public static void main(String []args){
        System.out.printf("主线程 %s 开始: %s \n", Thread.currentThread().getName(),getDate());
        FutureTaskDemo futureTaskDemo = new FutureTaskDemo();
        FutureTask futureTask = new FutureTask(futureTaskDemo, "done");
        Thread thread = new Thread(futureTask);
        thread.start();
        //我们可以尝试吧这一段 try catch 注释掉,看下结果
        try {
            System.out.printf("主线程: %s 时间: %s 结果: %s\n",
                    Thread.currentThread().getName(),getDate(), futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.printf("主线程 %s 结束: %s \n", Thread.currentThread().getName(),getDate());
    }
}

结果如下

主线程 main 开始: 2019-05-26 :05:29:25 
子线程 Thread-0 开始: 2019-05-26 :05:29:25 
子线程 Thread-0 结束: 2019-05-26 :05:29:30 
主线程: main 时间: 2019-05-26 :05:29:25 结果: done
主线程 main 结束: 2019-05-26 :05:29:30 

如果我们注释掉try catch,那么结果就会如下

主线程 main 开始: 2019-05-26 :05:36:16 
主线程 main 结束: 2019-05-26 :05:36:16 
子线程 Thread-0 开始: 2019-05-26 :05:36:16 
子线程 Thread-0 结束: 2019-05-26 :05:36:21 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容