큰 배열 에서 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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.