多線程 計算 1+2+3+4+.....+n 的值 — CountDownLatch

 CountDownLatch :一個同步輔助類,在完成一組正在其他線程中執行的操作之前,它允許一個或多個線程一直等待

用給定的計數 初始化 CountDownLatch。調用 countDown() 使當前計數減1,在到達0之前,await 方法會一直受阻塞。之後,會釋放所有等待的線程,await 的所有後續調用都將立即返回。

如 一個任務A,它要等待其他N個任務執行完畢之後才能執行,則可以用 CountDownLatch。

利用 CountDownLatch 計算  1+2+3+4+.....+n 的值,代碼如下:

package com.zewe.countdownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 多線程計算  1+2+3+4+5+......+n 的值
 * @author ZeWe
 *
 */
public class CountDownLatchTest {
	
	private static Integer sum = 0;
	private static Integer n = 10005;
	
	private static Integer latchSize = 10; //線程數量
	private static CountDownLatch latch;
	private static Lock lock;
	
	public static void main(String[] args) throws InterruptedException {

		lock = new ReentrantLock();
		latch = new CountDownLatch(latchSize);
		
		int avg = n / latchSize;
		int rem = n % latchSize;
		int left,right;
		for(int i = 1; i<=latchSize; i++) {
			left = (i-1)*avg+1;
			right = i == latchSize ? (i*avg+rem) : (i*avg);
			new Thread(new Run(left,right)).start();
		}
		
		latch.await(); // 等待10個進程完全結束,在進行主線程
		
		System.out.println(Thread.currentThread().getName()+"--> sum:"+sum);
		
	}
	
	
	
	
	
	static class Run implements Runnable{
	    private int left;
	    private int right;
		public Run(int left,int right) {
			this.left = left;
			this.right = right;
		}
		@Override
		public void run() {
			int res = 0;
			for(int i=left; i<=right; i++) {
				res+=i;
			}
			
			lock.lock(); // static sum 併發相加 加鎖 
			try {
				sum+=res;
				System.out.println(Thread.currentThread().getName()+"--> left:"+left+", right:"+right+", res:"+res+", sum: "+sum);
				latch.countDown(); // 完成一個 減去一個
			} finally {
				lock.unlock();
			}
			
		}
		
	}

}

結果:

Thread-0--> left:1, right:1000, res:500500, sum: 500500
Thread-9--> left:9001, right:10005, res:9550515, sum: 10051015
Thread-1--> left:1001, right:2000, res:1500500, sum: 11551515
Thread-2--> left:2001, right:3000, res:2500500, sum: 14052015
Thread-3--> left:3001, right:4000, res:3500500, sum: 17552515
Thread-4--> left:4001, right:5000, res:4500500, sum: 22053015
Thread-5--> left:5001, right:6000, res:5500500, sum: 27553515
Thread-6--> left:6001, right:7000, res:6500500, sum: 34054015
Thread-7--> left:7001, right:8000, res:7500500, sum: 41554515
Thread-8--> left:8001, right:9000, res:8500500, sum: 50055015
main--> sum:50055015

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章