Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Created September 22, 2013 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeilCli/6656758 to your computer and use it in GitHub Desktop.
Save MeilCli/6656758 to your computer and use it in GitHub Desktop.
あまり詳しくないけどExecutorServiceのサンプル
public class Worker{
private ExecutorService exec;
public ExecutorService getWorker(){
if(exec==null){
exec = Executors.newFixedThreadPool(2); // 上限2で作成
}
return exec;
}
public void test(){
Runnable run = new Runnable(){
@Override
public void run(){
// 処理
}
};
getWorker().submit(run);// このスレッドでは動作を待たない
Callable<Object> call = new Callable<Object>(){
@Override
public Object call() throws Exception{
// 処理
return null;
}
};
try{
Object obj = getWorker().submit(call).get();// get()で処理を待つ
}catch(InterruptedException e){
e.printStackTrace();
}catch(ExecutionException e){
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment