I just came across this code:
ExecutorService executorService = MoreExecutors.sameThreadExecutor(); for (int i = 0; i < 10; i++) { executorService.submit(new Callable<Void>() { @Override public Void call() throws Exception { try { Do some work here... return null; } catch (final Exception e) { throw e; } finally {
The difference between this and the code snippet below? If I understand it correctly, the sameThreadExecutor uses the same thread that calls submit (), which means that all these 10 "jobs" are started one after another in the main thread.
for (int i = 0; i < 10; i++) { try { Do some work here... } catch (final Exception e) { throw e; } finally {
Thanks!
source share