对于线程池execute以及submit你都了解吗?那么execute和submit这两者之间的区别是什么呢?很多人表示不大清楚,下面一起来了解一下。
代码:
public class ThreadPool_Test { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService pool = Executors.newCachedThreadPool(); pool.execute(new MyRunner()); Future < String > future = pool.submit(new MyCaller()); String ret = future.get(); System.out.println(ret); } } class MyCaller implements Callable < String > { @Override public String call() throws Exception { System.out.println("calling"); return "return_from_call"; } } class MyRunner implements Runnable { @Override public void run() { System.out.println("running"); } }
execute方法执行runnable 任务,submit方法执行callable任务,callable任务有返回值,但是,runnable不一样,runnable任务是void的,没有返回值。
// void java.util.concurrent.ThreadPoolExecutor final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts boolean completedAbruptly = true; try { while (task != null || (task = getTask()) != null) { w.lock(); if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } }
Runnable,task是MyRunner。
Callable,task是FutureTask。
submit方法的调用栈:
创建FutureTask对象,将Callable对象包裹起来,在run方法中调用Callable对象的方法,并且,设置返回值。
// java.util.concurrent.FutureTask.FutureTask public FutureTask(Callable < V > callable) { if (callable == null) throw new NullPointerException(); this.callable = callable; this.state = NEW; // ensure visibility of callable } public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset , null, Thread.currentThread())) return; try { Callable < V > c = callable; if (c != null && state == NEW) { V result; boolean ran; try { result = c.call(); ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) set(result); } } finally { // runner must be non-null until state is settled to // prevent concurrent calls to run() runner = null; // state must be re-read after nulling runner to prevent // leaked interrupts int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } }
线程池execute以及submit的区别你都了解了吗?在java入门学习当中,线程池方面的问题是经常会碰到的,希望上面的文章可以对你有所帮助,更多java方面的常见问题,请继续通过本站来进行了解吧。
推荐阅读: