Java之 java.util.concurrent 包之Executor與ExecutorService

[size=medium][b]一、問題: execute() 與 submit() 的區別?[/b][/size]

[b]execute() [/b]
來自 Executor 接口,
沒有返回值,
只接受 Runnable 對象。

[b]submit() [/b]
來自 ExecutorService 接口( ExecutorService 接口繼承了 Executor 接口)
返回 Future 對象
可以接受 Callable, Runnable 對象。


[size=medium][b]二、程序[/b][/size]




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系列文章

[url=http://lixh1986.iteye.com/blog/2341898]00_Java之 java.util.concurrent 包之概述[/url]

[url=http://lixh1986.iteye.com/blog/2360304]01_Java之java.util.concurrent包之Executor與ExecutorService[/url]

[url=http://lixh1986.iteye.com/blog/2360306]02_Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future[/url]

[url=http://lixh1986.iteye.com/blog/2351367]03_Java之多線程之Callable與Future[/url]

[url=http://lixh1986.iteye.com/blog/2351294]04_Java之多線程之Lock[/url]


轉載請註明,
原文出處:http://lixh1986.iteye.com/blog/2360304


-
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章