为什么newInstance在我的代码中抛出InstantiationException?

时间:2016-12-19 05:02:44

标签: java multithreading design-patterns thread-safety instantiationexception

我有两个过程,如下所示。我的每个流程都有runshutdown方法

Process processA = new ProcessA("processA", getProcessAProperties());
Process processB = new ProcessB("processB", getProcessBProperties());
  • 我希望每个进程都有不同的线程池配置,以便ProcessA在其自己的线程池中运行,而ProcessB在其自己的线程池中独立运行。
  • 我无法在自己的线程池的每个线程之间共享Process对象。

下面是我的Process类的样子,我的ProcessAProcessB类只是扩展了Process类。我在run方法中做了所有重要的事情。

public abstract class Process implements Runnable {
  private Properties props;
  private String processName;

  public Process(String processName, Properties props) {
    this.processName = processName;
    this.props = props;
  }

  protected abstract void shutdown();

  protected abstract void run(String processName, Properties props);

  @Override
  public final void run() {
    run(processName, props);
  }

  public Properties getProps() {
    return props;
  }

  public void setProps(Properties props) {
    this.props = props;
  }

  public String getProcessName() {
    return processName;
  }

  public void setProcessName(String processName) {
    this.processName = processName;
  }
}

以下是我如何使用自己的线程池运行ProcessA的简单示例。 有三个线程,每个线程都有自己的ProcessA对象可供使用。现在我想以更通用的方式扩展它,以便它可以同时用于我的进程ProcessA和{ {1}}。

ProcessB

因此,为了以更通用的方式解决这个问题,我创建了一个Process处理程序,如下所示:

public static void main(String[] args) {
  int numberOfThreads = 3;
  ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);

  final List<Process> processes = new ArrayList<>();
  for (int i = 0; i < numberOfThreads; i++) {
    // each thread works on different Process object
    Process processA = new ProcessA("processA", getProcessAProperties());
    processes.add(processA);
    executor.submit(processA);
  }

  Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
      for (Process process : processes) {
        process.shutdown();
      } 
      executor.shutdown();
      try {
        executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        e.printStackTrace;
      }
    }
  });
}

这就是我的主要方法现在的样子:

public final class ProcessHandler {
  private final ExecutorService executorServiceProcess;
  private final List<Process> processes = new ArrayList<>();
  private final Thread shutdownHook = new Thread() {
    @Override
    public void run() {
      for (Process process : processes)
        process.shutdown();
      executorServiceProcess.shutdown();
    }
  };

  public ProcessHandler(Process process, int poolSize) {
    this.executorServiceProcess = Executors.newFixedThreadPool(poolSize);
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    for (int i = 0; i < poolSize; i++) {
      try {
        // this line throws exception
        Process p = process.getClass().newInstance();
        p.setProcessName(process.getProcessName());
        p.setProps(process.getProps());
        processes.add(p);
        executorServiceProcess.submit(p);
      } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
      }
    }
  }

  public void shutdown() {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook.start();
    try {
      shutdownHook.join();
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }
  }
}

我的public static void main(String[] args) { Process processA = new ProcessA("processA", getProcessAProperties()); Process processB = new ProcessB("processB", getProcessBProperties()); // processA will run with three threads in its own thread pool ProcessHandler processHandlerA = new ProcessHandler (processA, 3); // processB will run with two threads in its own thread pool ProcessHandler processHandlerB = new ProcessHandler (processB, 2); // now I can call shutdown on them processHandlerA.shutdown(); processHandlerB.shutdown(); } ProcessHandler中的这一行抛出异常:

Process p = process.getClass().newInstance();

我不确定为什么会java.lang.InstantiationException: com.david.test.ProcessA 被抛出?

请注意:这些进程中的每一个都是kafka使用者,而且通常kafka使用者不是线程安全的,因此我每次都必须创建一个新对象并提交给执行者。

更新

这是我的ProcessA类看起来像:

InstantiationException

2 个答案:

答案 0 :(得分:1)

你是一个扩展你的Process抽象类的具体类吗?

抽象类无法自行实例化,请参阅:http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

也许尝试使用具体类扩展您的抽象类并创建具体类的实例,如果需要,您仍然可以将它们作为Process对象进行转换。

答案 1 :(得分:0)