如果你想收集並行線程的結果,我建議使用Future
抽象。特別是我會在FutureTask
實用程序的幫助下做到這一點。
public class RandomMatrixMinMax {
public static void main(String[] args) {
double maxGlob = 0.0;// max in all matrix
double minGlob = 1.0;// min in all matrix
final Double[][] x = generateData(100, 200);// generate matrix
final MinMaxFinderTask[] t = new MinMaxFinderTask[100];// make hundred
// threads -
// MinMaxFinder extends Thread
for (int i = 0; i < 100; i++) {
t[i] = new MinMaxFinderTask(x[i]);
new Thread(t[i]).start();
} // end of for
try {
for (int i = 0; i < 100; i++) {
if (t[i].get().getMax() > maxGlob) {
maxGlob = t[i].get().getMax();
}
if (t[i].get().getMin() < minGlob) {
minGlob = t[i].get().getMin();
}
}
} catch (final InterruptedException | ExecutionException e) {
}
// when done with all threads, print global max and min values for all
// matrix
System.out.println("Max is: " + maxGlob + " and min is: " + minGlob);
}// end of main
private static Double[][] generateData(int rows, int cols) {
final Double[][] randomMatrix = new Double[rows][cols];
final Random random = new Random();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
randomMatrix[j][i] = random.nextDouble();
}
}
return randomMatrix;
}
private static class MinMaxResult {
private Double min;
private Double max;
public MinMaxResult(Double min, Double max) {
this.min = min;
this.max = max;
}
public Double getMin() {
return min;
}
public void setMin(Double min) {
this.min = min;
}
public Double getMax() {
return max;
}
public void setMax(Double max) {
this.max = max;
}
}
private static class MinMaxFinderTask extends FutureTask<MinMaxResult> {
public MinMaxFinderTask(Double[] row) {
super(new MinMaxCalculator(row));
}
}
private static class MinMaxCalculator implements Callable<MinMaxResult> {
private final Double[] row;
public MinMaxCalculator(Double[] row) {
this.row = row;
}
@Override
public MinMaxResult call() throws Exception {
Double min = row[0];
Double max = row[0];
for (int i = 1; i < row.length; i++) {
if (row[i] < min) {
min = row[i];
}
if (row[i] > max) {
max = row[i];
}
}
return new MinMaxResult(min, max);
}
}
}
無論如何,我同意尼爾,這100個簡單任務的線程太多了。作爲一種替代ThreadPoolExecutor
可以委託計算的並行與Java引入了新的流API 8
在Java 8應用程序可以是:
public class RandomMatrixMinMax {
public static void main(String[] args) {
final Double[][] x = generateData(100, 200);
// obtain an array with min/max of each row of the matrix. The
// intermediate operation 'parallel' makes the computation parallel.
final MinMaxResult[] rowResults = Arrays.stream(x).parallel()
.map(row -> new MinMaxResult(Arrays.stream(row).min(Double::compare).get(),
Arrays.stream(row).max(Double::compare).get()))
.toArray(size -> new MinMaxResult[size]);
final Double maxGlob = Arrays.stream(rowResults).map(MinMaxResult::getMax).max(Double::compare).get();
final Double minGlob = Arrays.stream(rowResults).map(MinMaxResult::getMin).min(Double::compare).get();
System.out.println("Max is: " + maxGlob + " and min is: " + minGlob);
}
private static Double[][] generateData(int rows, int cols) {
final Double[][] randomMatrix = new Double[rows][cols];
final Random random = new Random();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
randomMatrix[j][i] = random.nextDouble();
}
}
return randomMatrix;
}
private static class MinMaxResult {
private Double min;
private Double max;
public MinMaxResult(Double min, Double max) {
this.min = min;
this.max = max;
}
public Double getMin() {
return min;
}
public void setMin(Double min) {
this.min = min;
}
public Double getMax() {
return max;
}
public void setMax(Double max) {
this.max = max;
}
}
}
這使我心情抑鬱,這種非問題了upvote。 – Raedwald