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 newScheduledThreadPool()用法及代码示例
- Java Executors newCachedThreadPool()用法及代码示例
- Java Executors defaultThreadFactory()用法及代码示例
- Java Executors newSingleThreadExecutor()用法及代码示例
- Java Executors newFixedThreadPool()用法及代码示例
- Java Executor newWorkStealingPool()用法及代码示例
- Java Executor privilegedThreadFactory()用法及代码示例
- Java Enum equals()用法及代码示例
- Java EnumSet range()用法及代码示例
- Java EnumMap remove()用法及代码示例
- Java Enum toString()用法及代码示例
- Java EnumMap hashCode()用法及代码示例
- Java EnumMap get()用法及代码示例
- Java EnumSet complementOf()用法及代码示例
- Java Enum ordinal()用法及代码示例
- Java EnumSet clone()用法及代码示例
- Java EnumMap entrySet()用法及代码示例
- Java EnumMap containsKey()用法及代码示例
- Java EnumMap containsValue(value)用法及代码示例
- Java Enumeration asIterator()用法及代码示例
注:本文由纯净天空筛选整理自 Java Executors Callable() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。