Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement parallel Calculator in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to implement parallel calculator in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Code implementation:

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 ("enter the number of addition calculators and multiplication calculators"); 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 plus_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 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 ()) {/ / the same number of multiplicative factors are sorted by the first different minimum 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 {/ / first factor private int firstNumber; / / second factor private int secondNumber; / / operation type: add, multiply, divide: 0 private int type 1 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;}} this is the answer to the question about how to implement parallel calculators in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report