美文网首页
FutureTask用法

FutureTask用法

作者: endlesswork | 来源:发表于2021-05-01 21:26 被阅读0次

    这里我们先举个例子

    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 
    

    相关文章

      网友评论

          本文标题:FutureTask用法

          本文链接:https://www.haomeiwen.com/subject/cgkxkltx.html