多线程计算1到100的求和
2017-12-09 16:01
211 查看
原文 http://blog.csdn.net/kiss_the_sun/article/details/49757285
问题:多线程计算1+2+…+100,如:起四个线程,分别计算1+2+..25, 26+27+…+50, 51+52+…+75, 76+77+…100, 最后将总和相关,输出应为5050
解决方法:
依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步
使用countDownLatch, 子线程执行完调用 countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步
使用cyclicbarrier, 子线程执行完调用 cyclicbarrier.await(), 最后都到达barrier时,输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步
通过线程池管理线程,用Future取得各子线程执行结果,最后将结果相加。
依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步。代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
输出:
from Thread-0 sum=325
from Thread-1 sum=1275
from Thread-2 sum=2850
from Thread-3 sum=5050
total result: 5050
子线程执行完调用 countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步,代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
输出:
from Thread-0 sum=325
from Thread-3 sum=2525
from Thread-2 sum=4100
from Thread-1 sum=5050
total result: 5050
子线程执行完调用 cyclicbarrier.await(), 最后都到达barrier时,输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步,代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
输出:
from Thread-0 sum=325
from Thread-2 sum=1900
from Thread-1 sum=2850
from Thread-3 sum=5050
5050
用Future取得各子线程执行结果,最后将结果相加。代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
输出:
from pool-1-thread-3 sum=1575
from pool-1-thread-1 sum=325
from pool-1-thread-2 sum=950
from pool-1-thread-4 sum=2200
total result: 5050
问题:多线程计算1+2+…+100,如:起四个线程,分别计算1+2+..25, 26+27+…+50, 51+52+…+75, 76+77+…100, 最后将总和相关,输出应为5050
解决方法:
依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步
使用countDownLatch, 子线程执行完调用 countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步
使用cyclicbarrier, 子线程执行完调用 cyclicbarrier.await(), 最后都到达barrier时,输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步
通过线程池管理线程,用Future取得各子线程执行结果,最后将结果相加。
使用thread.join
依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步。代码如下:package thread; public class ThreadAdd { public static int sum = 0; public static Object LOCK = new Object(); public static void main(String[] args) throws InterruptedException { ThreadAdd add = new ThreadAdd(); ThreadTest thread1 = add.new ThreadTest(1, 25); ThreadTest thread2 = add.new ThreadTest(26, 50); ThreadTest thread3 = add.new ThreadTest(51, 75); ThreadTest thread4 = add.new ThreadTest(76, 100); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread1.join(); thread2.join(); thread3.join(); thread4.join(); System.out.println("total result: "+sum); } class ThreadTest extends Thread { private int begin; private int end; @Override public void run() { synchronized (LOCK) { for (int i = begin; i <= end; i++) { sum += i; } System.out.println("from "+Thread.currentThread().getName()+" sum="+sum); } } public ThreadTest(int begin, int end) { this.begin = begin; this.end = end; } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
输出:
from Thread-0 sum=325
from Thread-1 sum=1275
from Thread-2 sum=2850
from Thread-3 sum=5050
total result: 5050
使用countDownLatch
子线程执行完调用 countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步,代码如下:package thread; import java.util.concurrent.CountDownLatch; public class ThreadAddLatch { public static int sum = 0; public static Object LOCK = new Object(); public static CountDownLatch countdown = new CountDownLatch(4); public static void main(String[] args) throws InterruptedException { ThreadAddLatch add = new ThreadAddLatch(); ThreadTest thread1 = add.new ThreadTest(1, 25); ThreadTest thread2 = add.new ThreadTest(26, 50); ThreadTest thread3 = add.new ThreadTest(51, 75); ThreadTest thread4 = add.new ThreadTest(76, 100); thread1.start(); thread2.start(); thread3.start(); thread4.start(); countdown.await(); System.out.println("total result: "+sum); } class ThreadTest extends Thread { private int begin; private int end; @Override public void run() { synchronized (LOCK) { for (int i = begin; i <= end; i++) { sum += i; } System.out.println("from "+Thread.currentThread().getName()+" sum="+sum); } countdown.countDown(); } public ThreadTest(int begin, int end) { this.begin = begin; this.end = end; } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
输出:
from Thread-0 sum=325
from Thread-3 sum=2525
from Thread-2 sum=4100
from Thread-1 sum=5050
total result: 5050
使用cyclicbarrier
子线程执行完调用 cyclicbarrier.await(), 最后都到达barrier时,输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步,代码如下:package thread; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class ThreadAddBarrier { public static int sum = 0; public static Object LOCK = new Object(); public static CyclicBarrier cyclicbarrier = new CyclicBarrier(4, new Runnable() { public void run() { System.out.println(sum); } }); public static void main(String[] args) throws InterruptedException { ThreadAddBarrier add = new ThreadAddBarrier(); ThreadTest thread1 = add.new ThreadTest(1, 25); ThreadTest thread2 = add.new ThreadTest(26, 50); ThreadTest thread3 = add.new ThreadTest(51, 75); ThreadTest thread4 = add.new ThreadTest(76, 100); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } class ThreadTest extends Thread { private int begin; private int end; @Override public void run() { synchronized (LOCK) { for (int i = begin; i <= end; i++) { sum += i; } System.out.println("from "+Thread.currentThread().getName()+" sum="+sum); } try { cyclicbarrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } } public ThreadTest(int begin, int end) { this.begin = begin; this.end = end; } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
输出:
from Thread-0 sum=325
from Thread-2 sum=1900
from Thread-1 sum=2850
from Thread-3 sum=5050
5050
通过线程池管理线程
用Future取得各子线程执行结果,最后将结果相加。代码如下:package thread; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ThreadAddFuture { public static List<Future> futureList=new ArrayList<Future>(); public static void main(String[] args) throws InterruptedException, ExecutionException { int sum=0; ThreadAddFuture add = new ThreadAddFuture(); ExecutorService pool=Executors.newFixedThreadPool(4); for(int i=1;i<=76;){ ThreadTest thread=add.new ThreadTest(i,i+24); Future<Integer> future=pool.submit(thread); futureList.add(future); i+=25; } if(futureList!=null && futureList.size()>0){ for(Future<Integer> future:futureList){ sum+=(Integer)future.get(); } } System.out.println("total result: "+sum); pool.shutdown(); } class ThreadTest implements Callable<Integer> { private int begin; private int end; public int sum=0; public ThreadTest(int begin, int end) { this.begin = begin; this.end = end; } @Override public Integer call() throws Exception { for(int i=begin;i<=end;i++){ sum+=i; } System.out.println("from "+Thread.currentThread().getName()+" sum="+sum); return sum; } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
输出:
from pool-1-thread-3 sum=1575
from pool-1-thread-1 sum=325
from pool-1-thread-2 sum=950
from pool-1-thread-4 sum=2200
total result: 5050
相关文章推荐
- JavaSE8基础 Callable 多线程求和计算
- 多线程例子_幂计算而后求和
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 考研小助手-用mathematica符号计算求和
- 编写一个java程序,从1加到100,结果是5050。 每做一次加法,就休眠1秒 当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去。
- 计算100以内所有正整数之和
- 计算100到200中间的素数的个数并输出
- 5. 多线程程序如何让 IO 和“计算”相互重叠,降低 latency?
- 不用循环计算1~100的和
- 0基础学编程之计算1-1/2+1/3-...-1/100
- Linux下使用POSIX Thread作多核多线程并行计算
- 计算1/1-1/2+1/3-1/4+1/5......+1/99-1/100的值
- 分布式计算:将Socket实验修改为服务端多线程程序,并测试完成
- 用于并行计算的多线程数据结构,
- 使用for循环,计算2+4+6+...+100
- iOS学习笔记-104.多线程03——线程间通信图片下载与时间计算
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性。 例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- 使用do while循环,计算2+4+6+...+100
- 计算1-100的和及其1-100内偶数的和(使用shell脚本来实现)
- 计算所有1-100之间数字之和(偶数之和)代码