Created
September 22, 2013 04:40
-
-
Save MeilCli/6656758 to your computer and use it in GitHub Desktop.
あまり詳しくないけどExecutorServiceのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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