简单解析execute和submit有什么区别

网友投稿 560 2023-03-14

简单解析execute和submit有什么区别

简单解析execute和submit有什么区别

1、execute 方法位于 java.util.concurrent.Executor 中

void execute(Runnable command);

2、execute 的具体实现

public void execute(Runnable command) {

if (command == null)

throw new NullPointerException();

/*

* Proceed in 3 steps:

*

* 1. If fewer than corePoolSize threads are running, try to

* start a new thread with the given command as its first

* task. The call to addWorker atomically checks runState and

* workerCount, and so prevents false alarms that would add

* threads when it shouldn't, by returning false.

*

* 2. If a task can be successfully queued, then we still need

* to double-check whether we should have added a thread

* (because existing ones died since last checking) or that

* the pool shut down since entry into this method. So we

* recheck state and if necessary roll back the enqueuing if

* stopped, or start a new thread if there are none.

*

* 3. If we cannot queue task, then we try to add a new

* thread. If it fails, we know we are shut down or saturated

* and so reject the task.

*/

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);

}

3、submit 方法位于 java.util.concurrent.AbstractExecutorService 中

/**

* @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;

}

4、submit 方式使用 Runnable 入参时的具体实现

static final class RunnableAdapter implements Callable {

final Runnable task;

final T result;

RunnableAdapter(Runnable task, T result) {

this.task = task;

this.result = result;

}

public T call() {

task.run();

return result;

}

}

5、submit 方式使用 Callable 入参时的具体实现

public FutureTask(Callable callable) {

if (callable == null)

throw new NullPointerException();

this.callable = calhttp://lable;

this.state = NEW; // ensure visibility omHVCdBOnuf callable

}

//重写run方法

public void run() {

if (state != NEW ||

!UNSAFE.compareAndSwapObject(this, runnerOffset,

null, Thread.currentThread()))

return;

try {

Callable 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);

}

}

总结:

1、根据源码可以看到 execute 仅可以接受Runnable类型,而 submit 重载了三个方法,参数可以是 Runnable 类型、Runnable 类型+泛型T 、Callable 类型接口。

2、从上面源码可以看出 submit 方法实际上如果用Runnable类型的接口可以有返回值,也可以没有返回值。

3、传递Runnable类型接口加泛型T会被进一步封装,在 Executors 这个类里面有个内部类 RunnableAdapter 实现了 Callable 接口。

4、看submit方法可以看出,submit最终也是在调用 execute 方法,无论是 Runnable 还是 Callable 类型接口,都会被封装成 FutureTask 继续执行。

5、如果使用submit方法提交,会进一步封装成FutureTask,执行execute方法,在FutureTask里面重写的run方法里面调用 Callable 接口的call方法。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序助手使用的图表插件(小程序图表组件 utils)
下一篇:平板电脑能打开小程序吗(平板电脑能用小程序吗)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~