Java之 java.util.concurrent 包之Executor与ExecutorService
程序员文章站
2022-04-04 09:09:25
...
一、问题: execute() 与 submit() 的区别?
execute()
来自 Executor 接口,
没有返回值,
只接受 Runnable 对象。
submit()
来自 ExecutorService 接口( ExecutorService 接口继承了 Executor 接口)
返回 Future 对象
可以接受 Callable, Runnable 对象。
二、程序
java.util.concurrent包之Execuotor系列文章
00_Java之 java.util.concurrent 包之概述
01_Java之java.util.concurrent包之Executor与ExecutorService
02_Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
03_Java之多线程之Callable与Future
04_Java之多线程之Lock
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2360304
-
execute()
来自 Executor 接口,
没有返回值,
只接受 Runnable 对象。
submit()
来自 ExecutorService 接口( ExecutorService 接口继承了 Executor 接口)
返回 Future 对象
可以接受 Callable, Runnable 对象。
二、程序
import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.Test; public class T01_Submit_VS_Execute { private Runnable runnable = new Runnabled(); private Callable<String> callable = new Callabled(); @Test public void testName() throws Exception { // public interface Executor // public interface ExecutorService extends Executor Executor e = Executors.newCachedThreadPool(); ExecutorService es = Executors.newScheduledThreadPool(10); // execute() // void java.util.concurrent.Executor.execute(Runnable command) //===================================================================================== // execute the given command (at some time in the future) with void return result. // e.execute(runnable); es.execute(runnable); // submit() // <T> Future<T> java.util.concurrent.ExecutorService.submit() //===================================================================================== /* submit and execute a value-returning task and returns a Future representing the pending results of the task. The Future's get() method will return the task's result upon successful completion. If you would like to immediately block and waiting for a task, you can use constructions of the form: result = exec.submit(aCallable).get(); */ Future<String> f1 = es.submit(callable); /* Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion. */ Future<?> f2 = es.submit(runnable); Future<String> f3 = es.submit(runnable, ""); // Waits (if necessary) for the computation to complete, and then retrieves its result. f1.get(); f2.get(); f3.get(); /** NOTE: Difference between "execute()" and "submit()" 1. void execute(): # execute a task, don't expect an execution result. 2. Future submit(): # execute a task, don't expect an execution result. OR # execute a task, expect a result. - call get() method immediately with blocking the thread. - store Future object first, then call get() method after all task complete. */ } class Runnabled implements Runnable{ @Override public void run() { } } class Callabled implements Callable<String>{ @Override public String call() throws Exception { return null; } } }
java.util.concurrent包之Execuotor系列文章
00_Java之 java.util.concurrent 包之概述
01_Java之java.util.concurrent包之Executor与ExecutorService
02_Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
03_Java之多线程之Callable与Future
04_Java之多线程之Lock
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2360304
-
下一篇: php /js 手机号码正则验证