zoukankan      html  css  js  c++  java
  • FutureTask demo

    package com.xinwei.order.entity;
    
    
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.Future;  
    
    public class CallableTest {  
          
        public static void main(String[] args) {  
            //创建线程池  
            ExecutorService es = Executors.newSingleThreadExecutor();  
            //创建Callable对象任务  
            CallableDemo calTask=new CallableDemo();  
            //提交任务并获取执行结果  
            Future<Integer> future =es.submit(calTask);  
            //关闭线程池  
            es.shutdown();  
            try {  
                Thread.sleep(2000);  
            System.out.println("主线程在执行其他任务");  
              
            if(future.get()!=null){  
                //输出获取到的结果  
                System.out.println("future.get()-->"+future.get());  
            }else{  
                //输出获取到的结果  
                System.out.println("future.get()未获取到结果");  
            }  
              
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            System.out.println("主线程在执行完成");  
        }  
    } 
    
    package com.xinwei.order.entity;
    
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    
    public class eeeee {  
          
        public static void main(String[] args) {  
            //创建线程池  
            ExecutorService es = Executors.newSingleThreadExecutor();  
            //创建Callable对象任务  
            CallableDemo calTask=new CallableDemo();  
            //创建FutureTask  
            FutureTask<Integer> futureTask=new FutureTask<>(calTask);  
            //执行任务  
            es.submit(futureTask);  
            //关闭线程池  
            es.shutdown();  
            try {  
                Thread.sleep(2000);  
            System.out.println("主线程在执行其他任务");  
              
            if(futureTask.get()!=null){  
                //输出获取到的结果  
                System.out.println("futureTask.get()-->"+futureTask.get());  
            }else{  
                //输出获取到的结果  
                System.out.println("futureTask.get()未获取到结果");  
            }  
              
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            System.out.println("主线程在执行完成");  
        }  
    } 
    import java.util.concurrent.Callable;  
    
    public class CallableDemo implements Callable<Integer> {  
          
        private int sum;  
        @Override  
        public Integer call() throws Exception {  
            System.out.println("Callable子线程开始计算啦!");  
            Thread.sleep(2000);  
              
            for(int i=1 ;i<100;i++){  
                sum=sum+i;  
            }  
            System.out.println("Callable子线程计算结束!");  
            return sum;  
        }  
    }  
  • 相关阅读:
    推荐阅读20100603
    [ASP.NET4之旅]Circular file references are not allowed
    满园尽是503,记曾经的一次IIS 7性能考验
    VS2010小Bug:找不到System.Web.Extensions.dll引用
    VS2010奇异Bug:三个中文符号在CSS文件中轻松让VS2010崩溃
    推荐阅读20100528
    Windows平台网站文件同步备份解决方案——cwRsyn
    推荐阅读20100803
    在IE8中使用建行企业网银的解决方法
    博客园已经用上NorthScale Memcached Server
  • 原文地址:https://www.cnblogs.com/alamps/p/7759848.html
Copyright © 2011-2022 走看看