您好,欢迎来到东饰资讯网。
搜索
您的当前位置:首页java submit execute_Java线程池中submit()和execute之间的区别?

java submit execute_Java线程池中submit()和execute之间的区别?

来源:东饰资讯网

一:

submit()方法,可以提供Future < T > 类型的返回值。

executor()方法,无返回值。

execute无返回值

public void execute(Runnable command) {

if (command == null)

throw new NullPointerException();//抛掉异常

int c = ctl.get();

if (workerCountOf(c) < corePoolSize) {

if (addWorker(command, true))

return;

c = ctl.get();

}

if (isRunning(c) && workQueue.offer(command)) {

int recheck = ctl.get();

if (! isRunning(recheck) && remove(command))

reject(command);

else if (workerCountOf(recheck) == 0)

addWorker(null, false);

}

else if (!addWorker(command, false))

reject(command);

}

submit有Future返回值 :

/**

* @throws RejectedExecutionException {@inheritDoc}

* @throws NullPointerException {@inheritDoc}

*/

public Future> submit(Runnable task) {

if (task == null) throw new NullPointerException();

RunnableFuture ftask = newTaskFor(task, null);

execute(ftask);

return ftask;

}

/**

* @throws RejectedExecutionException {@inheritDoc}

* @throws NullPointerException {@inheritDoc}

*/

public Future submit(Runnable task, T result) {

if (task == null) throw new NullPointerException();

RunnableFuture ftask = newTaskFor(task, result);

execute(ftask);

return ftask;

}

/**

* @throws RejectedExecutionException {@inheritDoc}

* @throws NullPointerException {@inheritDoc}

*/

public Future submit(Callable task) {

if (task == null) throw new NullPointerException();

RunnableFuture ftask = newTaskFor(task);

execute(ftask);

return ftask;

}

二:

excute方法会抛出异常。

sumbit方法不会抛出异常。除非你调用Future.get()。

三:

excute入参Runnable

submit入参可以为Callable,也可以为Runnable。

public interface Executor {

void execute(Runnable command);

}

public interface ExecutorService extends Executor {

...

Future submit(Callable task);

Future submit(Runnable task, T result);

Future> submit(Runnable task);

...

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuoyibo.cn 版权所有 湘ICP备2023022426号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务