今天看了看java的并行……写了一个实验品……
照书抄的,无须解释,看看估计就懂了……
但是把这个用在做题中会怎样呢?…… =_=
1 import java.util.concurrent.Executors;
2 import java.util.concurrent.ExecutorService;
3
4 class SumTask implements Runnable {
5 long Left;
6 long Right;
7 long ans;
8 final long MOD = 199999997;
9 SumTask(long L,long R) {
10 Left = L;
11 Right = R;
12 }
13 public void run() {
14 for (long i = Left; i < Right; i++) {
15 ans = (ans + i) % MOD;
16 }
17 System.out.println(ans);
18 }
19 }
20
21 public class mul {
22 public static void main(String args[]) {
23 long MOD = 199999997;
24 long ans = 0;
25 for (long i = 0; i < 400000000; i++) {
26 ans = (ans + i) % MOD;
27 }
28 System.out.println(ans);
29 SumTask task1 = new SumTask(0,100000000);
30 SumTask task2 = new SumTask(100000000,200000000);
31 SumTask task3 = new SumTask(200000000,300000000);
32 SumTask task4 = new SumTask(300000000,400000000);
33 System.out.println("4 threads Start!!");
34 ExecutorService runner = Executors.newFixedThreadPool(4);
35 runner.execute(task1);
36 runner.execute(task2);
37 runner.execute(task3);
38 runner.execute(task4);
39 runner.shutdown();
40 }
41 }