java - ThreadFactory 和 newThread(Runnable r) 如果它是线程,如何访问 r 的属性?

标签 java multithreading casting runnable executorservice

在我的论文中,我正在研究离散事件系统模拟器。模拟由一组 SimulatorThread extends Thread 组成,其操作包括将 Event 调度到 Simulator。每个 SimulatorThread 通过 SimulatorInterfaceSimulator 交互。

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执行每个线程。执行(模拟器线程)。我的问题是,在代码的某些部分,我需要获取对当前运行的 SimulatorThread 的引用,但是指令 (SimulatorThread) Thread.currentThread() 给出了一个转换异常。事实上 System.out.print(Thread.currentThread().getClass()) 的输出是 class java.lang.Thread,但我希望输出是class SimulatorThread 可以通过使用指令 simulatorThread.start() 运行线程而不是使用执行器来获得。所以我认为问题在于编写一个 ad-hoc ThreadFactory 返回 SimulatorThread 的实例。

事实上,我尝试使用简单的 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' 以创建一个 SimulatorThread >可运行?

最佳答案

如果我了解您的需求,正确的做法是扩展Thread,而是实现Runnable。然后可以享受您自己的类层次结构的所有好处:

public abstract class SimulatorRunnable extends Runnable {
     protected SimulatorInterface si;
     public SimulatorRunnable(SimulatorInterface si) {
         this.si = si;
     }
}

public final class Simulator extends SimulatorRunnable {
     public Simulator(SimulatorInterface si) {
         super(si);
     }
     public void run() {
         // here you can use the si
         si.simulate(...);
     }
}

然后将模拟器提交到线程池:

 Simulator simulator = new Simulator(si);
 ...
 exec.submit(simulator);

My problem is that in some part of the code i need to get a reference to the current running SimulatorThread, but the instruction (SimulatorThread) Thread.currentThread() gives a cast execption

您不应该将 Thread 传递给 ExecutorService。它只是将它用作 Runnable(因为 Thread 实现了 Runnable)并且线程池启动它自己的线程并且永远不会调用 start() 在你的 SimulatorThread 上。如果您正在扩展 Thread,那么您需要直接调用 thread.start() 并且将其提交给 ExecutorService。上述 implements RunnableExecutorService 的模式更好。

关于java - ThreadFactory 和 newThread(Runnable r) 如果它是线程,如何访问 r 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170947/

相关文章:

java - Gradle - spring-security-taglibs :4. 0.4.RELEASE 正在内部下载 spring-jdbc-4.2.5.RELEASE

java - 将Item添加到ArrayList的ArrayList中

java - 为什么虚拟线程(仅在短时间内 hibernate )会提高另一个线程的性能?

c# - 在我的多线程应用程序失败后,未完成的线程是否会挂起?

Java:从 Java 类返回相对 URL?

Java如何将UK转换为java.util.Date

java - 停止长时间等待的线程

c - ISO C++ 禁止比较指针和整数 : File handling in C error

c++ - C++中的隐式转换

CodeChef ATM 简单级别 : Uses Casting, 条件。代码不被接受