scala val numbers = (1 to 100).toVector val sum = numbers.par.map(_ * 2).reduce(_ + _) println(sum) public class Main { public static void main(String[] args) throws InterruptedException { int sum = 0; int[] numbers = new int[100]; for (int i = 0; i < numbers.length; i++) { numbers[i] = i + 1; } Thread[] threads = new Thread[10]; for (int i = 0; i < threads.length; i++) { final int start = i * 10; final int end = (i + 1) * 10; threads[i] = new Thread(() -> { for (int j = start; j < end; j++) { numbers[j] *= 2; } }); threads[i].start(); } for (Thread thread : threads) { thread.join(); } for (int number : numbers) { sum += number; } System.out.println(sum); } }


上一篇:
下一篇:
切换中文