Java中怎么实现并行计算器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
代码实现:
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; } LinkedList<Integer> plus_numbers = new LinkedList<>(); LinkedList<LinkedList<Integer>> 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<Integer> 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<Integer> plusNumbers, LinkedList<LinkedList<Integer>> 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<Integer> 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<Integer> mulNumberArray = mulNumbers.poll(); Integer first_mul_factor = mulNumberArray.poll(); Integer second_mul_factor = mulNumberArray.poll(); Future<Integer> 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<Integer> plusNumbers, final LinkedList<LinkedList<Integer>> 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<List<Integer>> {@Override public int compare(List<Integer> o1, List<Integer> 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 <Integer> {//第一因子 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中怎么实现并行计算器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。