• java 并发runable,callable,future,futureTask


     转载自:http://www.cnblogs.com/dolphin0520/p/3949310.html

    package future_call;
    
    import java.util.concurrent.Callable;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class Task implements Callable<Integer> {
      //  @Override
        public Integer call() throws Exception {
    
            System.out.println("子线程在进行计算");
    
            Thread.sleep(10000);
            System.out.println("子线程睡眠完毕");
            int sum=0;
            for (int i=0;i<100;i++){
    
                sum+=i;
    
            }
    
            return sum;
        }
    }

    1.使用Callable+Future

    package future_call;
    
    import java.util.concurrent.*;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class Task_test {
    
        public static void main(String [] args){
    
            ExecutorService executorService= Executors.newCachedThreadPool();
            Task task=new Task();
            Future<Integer> future=null;
            try{
               // executorService
    
                future=executorService.submit(task);
                System.out.println("线程池关闭之前");
                executorService.shutdown();
                System.out.println("线程池关闭之后");
    
    
    
    
            }catch (RejectedExecutionException e){System.out.println(e);}
    
    
            try{
    
               Thread.sleep(1000);
    
    
            }catch (InterruptedException e){e.printStackTrace();}
    
    
            System.out.println("主线程在进行计算");
    
    
            try{
    
                System.out.println(future.get());
    
    
            }catch (InterruptedException e){e.printStackTrace();} catch (ExecutionException e){e.printStackTrace();}
    
        }
    }

    2.使用Callable+FutureTask

    package future_call;
    
    import java.util.concurrent.*;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class future_task1 {
    
    
        public static void main(String [] args){
    
    
            ExecutorService executorService= Executors.newCachedThreadPool();
            Task task=new Task();
            FutureTask<Integer> futureTask=new FutureTask<Integer>(task);
            try {
                /*
                executorService.submit(futureTask);
                executorService.shutdown();
                */
                Thread thread=new Thread(futureTask);
                thread.start();
    
            }catch (RejectedExecutionException e){e.printStackTrace();}
    
            try {
                Thread.sleep(1000);
                System.out.println("主线程睡眠完毕");
            }catch (InterruptedException e){e.printStackTrace();}
    
    
    
            try{
    
                System.out.println(futureTask.get());
    
    
    
    
            }catch (InterruptedException e){e.printStackTrace();}catch (ExecutionException e){e.printStackTrace();}
    
    
    
        }
    
    
    
    }

     如果为了可取消性而使用 Future 但又不提供可用的结果,则可以声明 Future<?> 形式类型、并返回 null 作为底层任务的结果。

    /** 
                 * 提交runnable则没有返回值, future没有数据 
                 */  
                Future<?> result = mExecutor.submit(new Runnable() {  
      
                    @Override  
                    public void run() {  
                        fibc(20);  
                    }  
                });  
      
                System.out.println("future result from runnable : " + result.get());  
      
  • 相关阅读:
    Asp.net C# 图像处理
    java报表工具参数强化
    Aspose.Pdf for .NET控件PDF文档到Excel、EPS、SVG等的转换
    使用Solid Converter PDF控件把PDF转换至Word文档
    报表控件报表设计器工具QuickReport下载
    图形图像处理CAD控件Vectordraw Developer Framework下载
    Aspose.Words控件支持DOC,OOXML,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式
    自动验证编码值的条形码控件Barcode Professional SDK for .NET
    MVC技术的功能强大的报表创建和设计控件
    XML Barcode Webservice条形码控件介绍
  • 原文地址:https://www.cnblogs.com/luo-mao/p/7339333.html
Copyright © 2020-2023  润新知