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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。