Skip to content

Instantly share code, notes, and snippets.

@gustavopinto
Created July 28, 2011 01:26
Show Gist options
  • Select an option

  • Save gustavopinto/1110740 to your computer and use it in GitHub Desktop.

Select an option

Save gustavopinto/1110740 to your computer and use it in GitHub Desktop.
lucene-concurrent
private int doParallelTasks() throws Exception {
initTasksArray();
final int count [] = {0};
Thread t[] = new Thread [repetitions * tasks.size()];
// prepare threads
int indx = 0;
for (int k=0; k<repetitions; k++) {
for (int i = 0; i < tasksArray.length; i++) {
final PerfTask task = (PerfTask) tasksArray[i].clone();
t[indx++] = new Thread() {
public void run() {
try {
int n = task.runAndMaybeStats(letChildReport);
if (anyExhaustibleTasks)
updateExhausted(task);
synchronized (count) {
count[0] += n;
}
} catch (NoMoreDataException e) {
exhausted = true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
}
// run threads
startThreads(t);
// wait for all threads to complete
for (int i = 0; i < t.length; i++) {
t[i].join();
}
// return total count
return count[0];
}
private int doParallelTasks() throws Exception {
final TaskStats stats = getRunData().getPoints().getCurrentStats();
initTasksArray();
ParallelTask t[] = runningParallelTasks = new ParallelTask[repetitions * tasks.size()];
// prepare threads
int index = 0;
for (int k=0; k<repetitions; k++) {
for (int i = 0; i < tasksArray.length; i++) {
final PerfTask task = (PerfTask) tasksArray[i].clone();
t[index++] = new ParallelTask(task);
}
}
// run threads
startThreads(t);
// wait for all threads to complete
int count = 0;
for (int i = 0; i < t.length; i++) {
t[i].join();
count += t[i].count;
if (t[i].task instanceof TaskSequence) {
TaskSequence sub = (TaskSequence) t[i].task;
if (sub.countsByTime != null) {
if (countsByTime == null) {
countsByTime = new int[sub.countsByTime.length];
} else if (countsByTime.length < sub.countsByTime.length) {
countsByTime = ArrayUtil.grow(countsByTime, sub.countsByTime.length);
}
for(int j=0;j<sub.countsByTime.length;j++) {
countsByTime[j] += sub.countsByTime[j];
}
}
}
}
if (countsByTime != null) {
stats.setCountsByTime(countsByTime, logByTimeMsec);
}
// return total count
return count;
}
private class ParallelTask extends Thread {
public int count;
public final PerfTask task;
public ParallelTask(PerfTask task) {
this.task = task;
}
@Override
public void run() {
try {
int n = task.runAndMaybeStats(letChildReport);
if (anyExhaustibleTasks) {
updateExhausted(task);
}
count += n;
} catch (NoMoreDataException e) {
exhausted = true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment