次の SupervisedExecutor および ExecutorSuperviser の独自の実装を使用すると、パフォーマンスが低下するように思えます。このコードでは何が非効率的だと思いますか? どうすればその効率を改善できるかを知りたいです。
ExecutorSupervisor クラス:
public class ExecutorSuperviser {
private SupervisedExecutor[] threadPool;
private int poolSize = 0;
private LinkedList<Runnable> q;\\my own implementation of linkedlist
public ExecutorSuperviser(int nThreads) {
threadPool=new SupervisedExecutor[poolSize=nThreads];
q=new LinkedList<Runnable>();
init();
}
public void execute(Runnable r) {
synchronized (q) {
q.addToTail(r);
}
for (int i=0;i<poolSize;i++)
if (!threadPool[i].isBusy()) {
if (!threadPool[i].isAlive()) threadPool[i].start();
threadPool[i].interrupt();
return;
}
}
private void init() {
for (int i=0;i<poolSize;i++) {
threadPool[i]=new SupervisedExecutor(this);
}
}
public Object getLock() {
return q;
}
public Runnable getTask() {
return q.removeHead();
}
public void terminate() {
for (int i=0;i<poolSize;i++)
threadPool[i].terminate();
}
public void waitUntilFinished() {
while (!isFinished()) {
try {
Thread.sleep(Thread.MAX_PRIORITY);
} catch (InterruptedException e) {}
}
}
private boolean isFinished() {
for (int i=0;i<poolSize;i++)
if (threadPool[i].isBusy()) return false;
return q.isEmpty();
}
}
SupervisedExecutor クラス:
public class SupervisedExecutor extends Thread {
private boolean terminated = false;
private Boolean busy = false;
private ExecutorSuperviser boss;
SupervisedExecutor (ExecutorSuperviser boss) {
this.boss=boss;
}
public void run() {
while (!terminated) {
try {
sleep(MAX_PRIORITY);
} catch (InterruptedException e) {
synchronized (busy) {
busy=true;
}
Runnable r;
while (true) {
synchronized (boss.getLock()) {
r=boss.getTask();
}
if (r!=null) r.run();
else break;
}
synchronized (busy) {
busy=false;
}
}
}
}
public boolean isBusy() {
boolean isBusy;
synchronized (boss.getLock()) {
isBusy=busy;
}
return isBusy;
}
public void terminate() {
terminated=true;
}
}