并发问题请教

goInfall 2016-05-05 06:23:54
public class ThreadDemo {
public final ExecutorService threadPool = Executors.newCachedThreadPool();
public final CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool);
public static void main(String[] args) {
new ThreadDemo().solveCallableList();
}
public void solveCallableList() {
try {
int sum = 0;
completionService.submit(new TaskCallable(1));
completionService.submit(new TaskCallable(2));
completionService.submit(new TaskCallable(3));
for (int i = 0; i < 3; i++) {
int result = completionService.take().get();
sum += result;
}
System.out.println("sum = " + sum);
} catch (Exception e) {
e.printStackTrace();
}
}
public class TaskCallable implements Callable<Integer> {
private int options;
TaskCallable(int options) {
this.options = options;
}
@Override
public Integer call() throws Exception {
switch (options) {
case 1:
return Integer.valueOf(2 / 0);
case 2:
return Integer.valueOf(2 + 1);
case 3:
return Integer.valueOf(2 - 1);
default:
break;
}
return 0;
}
}
}
...全文
125 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
goInfall 2016-05-05
  • 打赏
  • 举报
回复
这是原来的代码,用了Executors的invokeAll方法,如果其中有一个任务有错误,那么就会抛出异常,但是我还想获得其他的结果值。
public class ThreadDemo {

	public final ExecutorService threadPool = Executors.newCachedThreadPool();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ThreadDemo().solveCallableList();
	}

	public void solveCallableList() {
		try {
			int sum = 0;
			List<Callable<Integer>> callableList = new ArrayList<Callable<Integer>>();
			callableList.add(new TaskCallable(1));
			callableList.add(new TaskCallable(2));
			callableList.add(new TaskCallable(3));
			List<Future<Integer>> futureList = threadPool.invokeAll(callableList, 2000L, TimeUnit.MILLISECONDS);
			if (futureList != null && futureList.size() > 0) {
				for (Future<Integer> future : futureList) {
					int result = future.get();
					sum += result;
				}
			}
			System.out.println("sum = " + sum);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class TaskCallable implements Callable<Integer> {
		private int options;

		TaskCallable(int options) {
			this.options = options;
		}

		@Override
		public Integer call() throws Exception {
			try{
				switch (options) {
				case 1:
					return Integer.valueOf(2 / 0);
				case 2:
					return Integer.valueOf(2 + 1);
				case 3:
					return Integer.valueOf(2 - 1);
				default:
					break;
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			return null;
		}
	}
}
这是改进之后的
public class ThreadDemo1 {
	public final ExecutorService threadPool = Executors.newCachedThreadPool();
	public final CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool);

	public static void main(String[] args) {
		new ThreadDemo1().solveCallableList();
	}

	public void solveCallableList() {
		try {
			int sum = 0;
			completionService.submit(new TaskCallable(1));
			completionService.submit(new TaskCallable(2));
			completionService.submit(new TaskCallable(3));
			for (int i = 0; i < 3; i++) {
				int result = completionService.take().get();
				sum += result;
			}
			System.out.println("sum = " + sum);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class TaskCallable implements Callable<Integer> {
		private int options;

		TaskCallable(int options) {
			this.options = options;
		}

		@Override
		public Integer call() throws Exception {
			try {
				switch (options) {
				case 1:
					return Integer.valueOf(2 / 0);
				case 2:
					return Integer.valueOf(2 + 1);
				case 3:
					return Integer.valueOf(2 - 1);
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return 0;
		}
	}
}
改进之后用ExecutorCompletionService可以获取其他的两个任务的结果值。
awusoft 2016-05-05
  • 打赏
  • 举报
回复
问题呢?凑字数
goInfall 2016-05-05
  • 打赏
  • 举报
回复
上面如何获取没有异常的另外两个任务的结果。

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧