큰 배열 에서 List 의 합 을 계산 합 니 다.

원본 주소:
http://www.iteye.com/topic/711162

package thread;

import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CyclicBarrier;

public class SegmentCount {
	public static void main(String[] args) {
		int[] arr = new int[10123];
		Random ran = new Random();
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			arr[i] = ran.nextInt(10000);
			sum += arr[i];
		}
		System.out.println("     :" + sum);

		//            
		int threadSize = 10;
		int avg = arr.length / threadSize;//      
		int surplus = arr.length % threadSize;//      
		threadSize = surplus > 0 ? (++threadSize) : threadSize;
		final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();//     
		CyclicBarrier barrier = new CyclicBarrier(threadSize, new Runnable() {
			public void run() {
				System.out.println("  :");
				System.out.println(queue);
				int sum = 0;
				Iterator<Integer> it = queue.iterator();
				while (it.hasNext()) {
					sum += Integer.valueOf(it.next().toString());
				}
				System.out.println("sum:" + sum);
			}
		});

		for (int i = 1; i <= threadSize; i++) {
			int end = i * avg;
			end = (end > arr.length) ? arr.length : end;
			new Thread(new Segment(barrier, arr, (i - 1) * avg, end, queue))
					.start();
		}
	}
}

class Segment implements Runnable {
	CyclicBarrier barrier;
	int[] arr;
	int start;
	int end;
	ConcurrentLinkedQueue<Integer> queue;

	public Segment(CyclicBarrier barrier, int[] arr, int start, int end,
			ConcurrentLinkedQueue<Integer> queue) {
		super();
		this.barrier = barrier;
		this.arr = arr;
		this.start = start;
		this.end = end;
		this.queue = queue;
	}

	int sum = 0;

	public void run() {
		System.out.println(Thread.currentThread().getName() + ",start:" + start
				+ ",end:" + end);
		for (int i = start; i < end; i++) {
			sum += arr[i];
		}
		queue.offer(sum);
		try {
			barrier.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
	}
}


package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class SegmentCount2 {
	public static void main(String[] args) throws InterruptedException,
			ExecutionException {
		int[] arr = new int[10123];
		Random ran = new Random();
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			arr[i] = ran.nextInt(10000);
			sum += arr[i];
		}
		System.out.println("     :" + sum);

		//            

		int threadSize = 10;
		int avg = arr.length / threadSize;//      
		int surplus = arr.length % threadSize;//      
		threadSize = surplus > 0 ? (++threadSize) : threadSize;

		ExecutorService pool = Executors.newCachedThreadPool();
		List<Segment2> tasks = new ArrayList<Segment2>();
		for (int i = 1; i <= threadSize; i++) {
			int end = i * avg;
			end = (end > arr.length) ? arr.length : end;
			tasks.add(new Segment2(arr, (i - 1) * avg, end));
		}
		//        ,        ,             Future   。
		List<Future<Integer>> result = pool.invokeAll(tasks);

		int summ = 0;
		for (int i = 0; i < result.size(); i++) {
			FutureTask<Integer> task = (FutureTask<Integer>) result.get(i);
			summ += Integer.valueOf(task.get().toString());
		}
		System.out.println("  :" + summ);
		pool.shutdown();
	}
}

class Segment2 implements Callable<Integer> {
	int[] arr;
	int start;
	int end;

	public Segment2(int[] arr, int start, int end) {
		super();
		this.arr = arr;
		this.start = start;
		this.end = end;
	}

	int sum = 0;

	public Integer call() throws Exception {
		System.out.println(Thread.currentThread().getName() + ",start:" + start
				+ ",end:" + end);
		for (int i = start; i < end; i++) {
			sum += arr[i];
		}
		return sum;
	}
}


package thread;

import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

public class SegmentCount3 {
	public static void main(String[] args) throws InterruptedException,
			ExecutionException {
		int[] arr = new int[10123];
		Random ran = new Random();
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			arr[i] = ran.nextInt(10000);
			sum += arr[i];
		}
		System.out.println("     :" + sum);

		//            

		int threadSize = 10;
		int avg = arr.length / threadSize;//      
		int surplus = arr.length % threadSize;//      
		threadSize = surplus > 0 ? (++threadSize) : threadSize;
		final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();//     
		CountDownLatch beginSignal = new CountDownLatch(1);
		CountDownLatch endSignal = new CountDownLatch(threadSize);

		for (int i = 1; i <= threadSize; i++) {
			int end = i * avg;
			end = (end > arr.length) ? arr.length : end;
			new Thread(new Segment3(beginSignal, endSignal, arr, (i - 1) * avg,
					end, queue)).start();
		}
		System.out.println("  ");
		beginSignal.countDown();
		endSignal.await();
		System.out.println("  :" + queue);
		Iterator<Integer> it = queue.iterator();
		int summ = 0;
		while (it.hasNext()) {
			summ += it.next();
		}
		System.out.println(summ);
	}
}

class Segment3 implements Runnable {
	CountDownLatch beginSignal;
	CountDownLatch endSignal;
	int[] arr;
	int start;
	int end;
	ConcurrentLinkedQueue<Integer> queue;

	public Segment3(CountDownLatch beginSignal, CountDownLatch endSignal,
			int[] arr, int start, int end, ConcurrentLinkedQueue<Integer> queue) {
		super();
		this.beginSignal = beginSignal;
		this.endSignal = endSignal;
		this.arr = arr;
		this.start = start;
		this.end = end;
		this.queue = queue;
	}

	int sum = 0;

	public void run() {
		try {
			beginSignal.await();

			System.out.println(Thread.currentThread().getName() + ",start:"
					+ start + ",end:" + end);

			for (int i = start; i < end; i++) {
				sum += arr[i];
			}
			queue.offer(sum);
			endSignal.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}


좋은 웹페이지 즐겨찾기