ExecutorService的execute和submit方法
2013-10-20 01:08
453 查看
/*这个是今天question 1的两个例子*/
/*题目;The first example uses a threads to execute 4 instances of the
* Runnable interface T1.Task T1 is implemented as an implementation of the Runnable
* interface and the code is contained in the run method .In this simple example the task
* is to print a number 10 times on the screen followed by a newline. The array of class
* Future is used to hold references to each task submitted to the pool of thread.In this
* case 4 jobs are going to be submitted and ,hence,the array has size 4.Once the jobs
* are submitted we want main() to wait for all the tasks in the threadpool to complete.
* This is done by invoking get on each job submitted.Then the pool is shut down. */
import java.util.concurrent.*;
class ThreadPool1 {
public static void main(String arg[]){
ExecutorService pool;
pool=Executors.newFixedThreadPool(2);
Runnable r;
Future f[]=new Future[4];
for(int j=0;j<4;j++){
r=new T1(j);
f[j]=pool.submit(r);
}
/*下面一段代码是question1的下半截用到的*/
try{
for(Future x: f)x.get();
}
catch(InterruptedException e){}
catch(ExecutionException e){}
pool.shutdown();
System.out.println("Main finished");
}
}
class T1 implements Runnable{
int k;
public T1(int kk){
k=kk;
}
public void run(){
for(int j=0;j<10;j++);
System.out.println();
}
}
===================================解决方案========================================================
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorServiceTest {
/*
* 因为之前一直用的execute方法,最近有个情况需要 用到submit方法,
*/
/*
* 接收的参数不一样 submit有返回值,而execute没有
*
* Method submit extends base method Executor. execute by creating and
* returning a Future that can be used to cancel execution and/or wait for
* completion.
*
* 用到返回值的例子,比如说我有很多个做validation的task,我希望所有的task执行完,
* 然后每个task告诉我它的执行结果,是成功还是失败,如果是失败. 原因是什么: 然后我就可以把所有失败的原因综合起来发给调用者。
*
*
* 个人觉得cancel executio这个用处不大,很少有需要去取消执行的。
*
* 3,submit方便Exception处理 意思就是如果你在你的task里会抛出checked或者unchecked
* exception,而你又希望 外面的调用者能够感知这些exception并做出及时的处理,那么就需要用到submit,通过
* 捕获Future.get抛出的异常。
*
*
*/
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<String>> resultList = new ArrayList<Future<String>>();
// 创建10个任务并执行
for (int i = 0; i < 4; i++) {
// 使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
Future<String> future = (Future<String>) executorService
.submit(new Task(i));
// 将任务执行结果存储到List中
resultList.add(future);
}
// 1、shutdown方法:这个方法会平滑地关闭ExecutorService,当我们调用这个方法时,
// ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任
// 务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经
// 提交的任务执行完毕后将会关闭ExecutorService。这里我们先不举例在下面举例
// 遍历任务的结果
for (Future<String> fs : resultList) {
try {
// 打印各个线程(任务)执行的结果
System.out.println(fs.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
return;
}
}
executorService.shutdown();
}
}
class Task implements Runnable {
private int id;
public Task(int id) {
this.id = id;
}
/*
* 任务的具体过程,一旦任务传递ExecutorService 的 submit方法,
* 则该方法自动在一个线程上执行。
*/
public void run() {
System.out.println("run()方法被自动调用,干活!!!"+Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
System.out.println(
""+id+" "+Thread.currentThread().getName()
);
}
}
}
/*题目;The first example uses a threads to execute 4 instances of the
* Runnable interface T1.Task T1 is implemented as an implementation of the Runnable
* interface and the code is contained in the run method .In this simple example the task
* is to print a number 10 times on the screen followed by a newline. The array of class
* Future is used to hold references to each task submitted to the pool of thread.In this
* case 4 jobs are going to be submitted and ,hence,the array has size 4.Once the jobs
* are submitted we want main() to wait for all the tasks in the threadpool to complete.
* This is done by invoking get on each job submitted.Then the pool is shut down. */
import java.util.concurrent.*;
class ThreadPool1 {
public static void main(String arg[]){
ExecutorService pool;
pool=Executors.newFixedThreadPool(2);
Runnable r;
Future f[]=new Future[4];
for(int j=0;j<4;j++){
r=new T1(j);
f[j]=pool.submit(r);
}
/*下面一段代码是question1的下半截用到的*/
try{
for(Future x: f)x.get();
}
catch(InterruptedException e){}
catch(ExecutionException e){}
pool.shutdown();
System.out.println("Main finished");
}
}
class T1 implements Runnable{
int k;
public T1(int kk){
k=kk;
}
public void run(){
for(int j=0;j<10;j++);
System.out.println();
}
}
===================================解决方案========================================================
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorServiceTest {
/*
* 因为之前一直用的execute方法,最近有个情况需要 用到submit方法,
*/
/*
* 接收的参数不一样 submit有返回值,而execute没有
*
* Method submit extends base method Executor. execute by creating and
* returning a Future that can be used to cancel execution and/or wait for
* completion.
*
* 用到返回值的例子,比如说我有很多个做validation的task,我希望所有的task执行完,
* 然后每个task告诉我它的执行结果,是成功还是失败,如果是失败. 原因是什么: 然后我就可以把所有失败的原因综合起来发给调用者。
*
*
* 个人觉得cancel executio这个用处不大,很少有需要去取消执行的。
*
* 3,submit方便Exception处理 意思就是如果你在你的task里会抛出checked或者unchecked
* exception,而你又希望 外面的调用者能够感知这些exception并做出及时的处理,那么就需要用到submit,通过
* 捕获Future.get抛出的异常。
*
*
*/
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<String>> resultList = new ArrayList<Future<String>>();
// 创建10个任务并执行
for (int i = 0; i < 4; i++) {
// 使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
Future<String> future = (Future<String>) executorService
.submit(new Task(i));
// 将任务执行结果存储到List中
resultList.add(future);
}
// 1、shutdown方法:这个方法会平滑地关闭ExecutorService,当我们调用这个方法时,
// ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任
// 务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经
// 提交的任务执行完毕后将会关闭ExecutorService。这里我们先不举例在下面举例
// 遍历任务的结果
for (Future<String> fs : resultList) {
try {
// 打印各个线程(任务)执行的结果
System.out.println(fs.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
return;
}
}
executorService.shutdown();
}
}
class Task implements Runnable {
private int id;
public Task(int id) {
this.id = id;
}
/*
* 任务的具体过程,一旦任务传递ExecutorService 的 submit方法,
* 则该方法自动在一个线程上执行。
*/
public void run() {
System.out.println("run()方法被自动调用,干活!!!"+Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
System.out.println(
""+id+" "+Thread.currentThread().getName()
);
}
}
}
相关文章推荐
- ExecutorService的execute和submit方法
- (转)ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法
- 0007 ExecutorService的execute和submit方法【基础】
- ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法
- ExecutorService的submit和execute方法
- Java ExecutorService中execute()和submit()方法的区别
- ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法的区别
- ExecutorService的execute和submit方法区别
- ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法
- ExecutorService的execute和submit方法
- ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别
- ExecutorService的execute和submit方法
- 关于ExecutorService中的execute()和submit()方法的区别
- ExecutorService的execute和submit方法
- ExecutorService中submit和execute的区别