当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Executors Callable()用法及代码示例


Executors 类的 Callable() 方法返回一个 Callable 对象,该对象在调用时运行给定的任务并返回 null。

用法

public static Callable<Object> callable(Runnable task)
public static <T> Callable<T> callable(Runnable task, T result)
public static Callable<Object> callable(PrivilegeAction<?> action)
public static Callable<Object> callable(PrivilegedExceptionAction<?> action)

参数

任务 - 要运行的任务

结果 - 要返回的结果

action - 要运行的特权操作

返回

一个可调用的对象

抛出

NullPointerException

例子1

import java.util.ArrayList;  
    import java.util.Date;  
    import java.util.List;  
    import java.util.concurrent.Callable;  
    import java.util.concurrent.ExecutionException;  
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.Future;  
      
    public class ExecutorCallableExample1 implements Callable<String> {  
      
        @Override  
        public String call() throws Exception {  
            Thread.sleep(1000);  
            return Thread.currentThread().getName();  
        }  
          
        public static void main(String args[]){  
      
            ExecutorService executor = Executors.newFixedThreadPool(20);  
            List<Future<String>> list = new ArrayList<Future<String>>();  
            Callable<String> callable = new ExecutorCallableExample1();  
      
            for(int i=0; i< 10; i++){  
                Future<String> ftr = executor.submit(callable);  
                list.add(ftr);  
            }  
            for(Future<String> fut:list){  
                try {  
                    System.out.println(new Date()+ "::"+fut.get());  
      
                } catch (InterruptedException | ExecutionException e) {  
                    e.printStackTrace();  
                }  
            }  
            executor.shutdown();  
        }  
      
    }

输出:

Sun Mar 17 02:29:46 IST 2019::pool-1-thread-1
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-2
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-3
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-4
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-5
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-6
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-7
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-8
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-9
Sun Mar 17 02:29:48 IST 2019::pool-1-thread-10

例子2

import java.util.ArrayList;  
    import java.util.Date;  
    import java.util.List;  
    import java.util.concurrent.Callable;  
    import java.util.concurrent.ExecutionException;  
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.Future;  
      
    public class ExecutorCallableExample2 implements Callable<String> {  
      
        @Override  
        public String call() throws Exception {  
            Thread.sleep(500);  
            return Thread.currentThread().getName();  
        }     
        public static void main(String args[]){  
      
            ExecutorService executor = Executors.newFixedThreadPool(20);  
            List<Future<String>> lst = new ArrayList<Future<String>>();  
            Callable<String> clbl = new ExecutorCallableExample1();  
               int i =0;
            while( i < 5){  
                Future<String> ftr = executor.submit(clbl);  
                lst.add(ftr);
                i++;
            }  
            for(Future<String> fut:lst){  
                try {  
                    System.out.println( "::"+fut.get());  
      
                } catch (InterruptedException | ExecutionException e) {  
                    e.printStackTrace();  
                }  
            }  
            executor.shutdown();  
        }  
      
    }

输出:

::pool-1-thread-1
::pool-1-thread-2
::pool-1-thread-3
::pool-1-thread-4
::pool-1-thread-5




相关用法


注:本文由纯净天空筛选整理自 Java Executors Callable() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。