使用Callable来获取数组的最大值
来源:互联网 发布:单片机程序设计师培训 编辑:程序博客网 时间:2024/05/18 11:47
首先创建实现Callable的类,代码如下:
import java.util.concurrent.Callable;/** * Created by ea on 2017/3/13. */public class TestCallable implements Callable<Integer>{ private int[] arr; private int start, end; public TestCallable(int[] a, int start, int end) { arr = a; this.start = start; this.end = end; } public Integer call() { int ans = Integer.MIN_VALUE; for (int i = start; i < end; i++) { ans = Math.max(ans, arr[i]); } return ans; }}然后在主程序中创建ExecutorService,提交任务,获取计算结果。
import java.util.concurrent.*;public class Main { public static void main(String[] args) { int[] a = {4, 5, 6, 1, 2, 3}; TestCallable call1 = new TestCallable(a, 0, a.length / 2); TestCallable call2 = new TestCallable(a, a.length / 2 + 1, a.length); ExecutorService s = Executors.newFixedThreadPool(3); Future<Integer> t1 = s.submit(call1); Future<Integer> t2 = s.submit(call2); try { int ans = Math.max(t1.get(), t2.get()); System.out.println("ans:" + ans); } catch(InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }}
0 0
- 使用Callable来获取数组的最大值
- iOS优雅的获取数组的最大值
- 获取数组中子串乘积的最大值
- java 获取数组的最大值和最小值
- 获取数组中的最大值
- java获取数组最大值
- 获取数组最大值
- 获取数组最大值
- 获取数组中的最大值
- js获取数组最大值
- 获取数组中最大值
- 利用Math对象,获取数组的最大值最小值,获取随机数
- 使用callable获取子线程的返回值
- Java之数组获取最大值
- 获取数组最大值跟最小值
- js 获取数组中最大值
- 数组获取最大值和最小值
- js 获取数组最小/最大值
- Mac下安装MongoDB
- PHP——CURL模拟请求微信接口
- 常用工具
- MyGitHub
- DESFire 学习之AUTH
- 使用Callable来获取数组的最大值
- 数据库类型
- Makefile学习
- JAVA中多线程同步的方法
- BZOJ 2696 航班安排 费用流
- SVN连接失败的解决办法
- 深入理解C#---2.事件?委托?傻傻分不清楚
- 20170313一个很奇怪的命令
- menu菜单