Java中怎么实现并行计算器
Java中怎么实现并行计算器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联是一家专业提供义乌企业网站建设,专注与成都网站制作、成都做网站、外贸营销网站建设、HTML5、小程序制作等业务。10年已为义乌众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
代码实现:
package test;import java.util.*;import java.util.concurrent.*;public class Main { final static ExecutorService executorService = Executors.newCachedThreadPool(); public static void main(String [] args) throws ExecutionException, InterruptedException { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { System.out.println("请输入加法计算器和乘法计算器个数"); String inputStr = scanner.nextLine(); String[] operateNumArry = scanner.nextLine().split(" "); int plusNum = Integer.parseInt(operateNumArry[0]); int mulNum = Integer.parseInt(operateNumArry[1]); if (plusNum < 0 || mulNum < 0) { System.out.println("并行计算器数量要大于0"); break; } LinkedListplus_numbers = new LinkedList<>(); LinkedList > mul_number_array = new LinkedList<>(); String [] inputArray = inputStr.split("\\+"); for (int i = 0; i < inputArray.length; i++) {if (!inputArray[i].contains("*")) { plus_numbers.add(Integer.parseInt(inputArray[i])); } else { LinkedList mul_numbers = new LinkedList<>(); String [] mul_numbers_str = inputArray[i].split("\\*"); for (int j = 0; j < mul_numbers_str.length; j++) { mul_numbers.add(Integer.parseInt(mul_numbers_str[j])); }//乘排序 Collections.sort(mul_numbers); //添加到list mul_number_array.add(mul_numbers); } } StringBuilder builder = new StringBuilder(); calculateSum(plus_numbers, mul_number_array, plusNum, mulNum,builder); System.out.println(builder.toString()); } } /** * 計算 * @param plusNumbers * @param mulNumbers * @param plusNum * @param mulNum */ public static void calculateSum(LinkedList plusNumbers, LinkedList > mulNumbers, int plusNum, int mulNum, StringBuilder builder) throws ExecutionException, InterruptedException {//首先打印 printResult(plusNumbers, mulNumbers, builder); if (plusNumbers.size() == 1) {return; } //計算加 int requiredPlusNum = plusNumbers.size(); if (requiredPlusNum > plusNum) requiredPlusNum = plusNum; else if (requiredPlusNum <= plusNum) requiredPlusNum--; for (int i = 0; i < requiredPlusNum; i++) { Integer first_plus_factor = plusNumbers.poll(); Integer second_plus_factor = plusNumbers.poll(); Future future = executorService.submit(new Operator(first_plus_factor, second_plus_factor, 0)); plusNumbers.addLast(future.get()); } //计算乘 int requiredMulNum = mulNumbers.size(); if (requiredMulNum > mulNum) requiredMulNum = mulNum; for (int i = 0; i < requiredMulNum; i++) { LinkedList mulNumberArray = mulNumbers.poll(); Integer first_mul_factor = mulNumberArray.poll(); Integer second_mul_factor = mulNumberArray.poll(); Future future = executorService.submit(new Operator(first_mul_factor, second_mul_factor, 2)); if (mulNumberArray.isEmpty()) plusNumbers.add(future.get()); else { mulNumberArray.offer(future.get()); mulNumbers.offer(mulNumberArray); } } //递归调用 calculateSum(plusNumbers, mulNumbers, plusNum, mulNum, builder); } static void printResult(final LinkedList plusNumbers, final LinkedList > mulNumbers, StringBuilder builder) {//加排序 Collections.sort(plusNumbers); builder.append("{"); plusNumbers.forEach(item-> {builder.append(item).append("+"); }); if (!mulNumbers.isEmpty()) {//相同數量的乘因子按第一個不同的最小值排序 Collections.sort(mulNumbers, new CustomComparator()); mulNumbers.forEach(item->{ item.forEach(i->{builder.append(i).append("*"); }); builder.deleteCharAt(builder.lastIndexOf("*")).append("+"); }); } builder.deleteCharAt(builder.lastIndexOf("+")).append("}").append("\n"); } static class CustomComparator implements Comparator > {@Override public int compare(List
o1, List o2) {if (o1.size() == o2.size()) {for (int i = 0; i < o1.size(); i++) {if (o1.get(i) > o2.get(i))return 1; else if (o1.get(i) < o2.get(i))return -1; else return 0; } return 0; } else if (o1.size() > o2.size()){return 1; } return -1; } } static class Operator implements Callable {//第一因子 private int firstNumber; //第二因子 private int secondNumber; //操作類型:加、減、乘、除:0/1/2/3 private int type; public Operator(int firstNumber, int secondNumber, int type) {this.secondNumber = secondNumber; this.firstNumber = firstNumber; this.type = type; } public Integer call() throws Exception {if (type == 0)return firstNumber + secondNumber; if (type == 1)return firstNumber - secondNumber; if (type == 2)return firstNumber * secondNumber; if (type == 3)return firstNumber / secondNumber; if (type == 4)return firstNumber % secondNumber; return 0; } } }
关于Java中怎么实现并行计算器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
新闻名称:Java中怎么实现并行计算器
本文URL:http://pwwzsj.com/article/ijeidg.html