私の論文では、Discrete Event System Simulator に取り組んでいます。シミュレーションは のセットで構成され、SimulatorThread extends Threadそのアクションは を にスケジューリングすることで構成されEventますSimulator。それぞれがを通じてSimulatorThreadと相互作用します。SimulatorSimulatorInterface
public abstract class SimulatorThread extends Thread {
private SimulatorInterface si;
public SimulatorThread(SimulatorInterface si) {
this.si = si;
}
...
}
public final class Simulator {
private ExecutorService exec;
...
public void assignThread(SimulatorThread... stList) {
...
}
}
シミュレーションが開始される前に、それぞれSimulatorThreadが に割り当てられ、 はSimulatorを通じてSimulator各スレッドを実行しますexec.execute(simulatorThread)。私の問題は、コードの一部で現在実行SimulatorThread中のへの参照を取得する必要があることですが、命令(SimulatorThread) Thread.currentThread()はキャスト実行を与えます。実際の出力はSystem.out.print(Thread.currentThread().getClass())is ですが、エグゼキューターを使用する代わりに命令を使用してスレッドを実行することで出力を取得できるclass java.lang.Threadようにしたいと思います。そのため、問題はのインスタンスを返すアドホックを書くことにあると思いました。class SimulatorThreadsimulatorThread.start() ThreadFactorySimulatorThread
実際、私は些細なことを使用しようとしましたSimulatorThreadFactory extends ThreadFactory:
public class SimulatorThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
return new SimulatorThread(new SimulatorInterface());
}
}
これにより、以前に引用した出力「class SimulatorThread」を取得しました。問題は、'exec.execute(simulatorThread)' を呼び出すと、パラメーターにはアクセスする必要がある属性 'SimulatorInterface' がありますが、メソッド 'newThread' のパラメーターが 'Runnable' であるため、アクセスできません。 '。ここで、私が言葉で説明するよりも、私が意味することをよりよく表現していることを願って、間違ったコードを公開します。
public class SimulatorThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
SimulatorInterface si = r.getSimulatorInterface(); // this is what
// I would like
// the thread factory
// to do
return new SimulatorThread(si);
}
}
では、パラメータが である場合newThreadに を作成するために、メソッド内の「SimulatorThread」の属性「SimulatorInterface」にアクセスするにはどうすればよいですか?SimulatorThreadRunnable