InterruptedException和Class.newInstance()的无法访问的catch块

时间:2017-02-28 01:41:31

标签: java multithreading interrupt interrupt-handling interrupted-exception

在当前项目中,我试图加载一组插件;问题是插件可以做任何事情,包括抛出异常,或者只是永远挂起。我想完成以下任务:

try {

    // load class by package name
    Class<?> pluginClass = Class.forName(plginClassName);

    // create a new instance of the object
    // this call could throw an exception or never return
    GenericPlugin plugin = (GenericPlugin) pluginClass.newInstance();

    state = PluginState.INITIALIZED;
    ...

} catch ( InterruptedException ie ) {  // <-- Compile Error Here
// Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body

    state = PluginState.TIMEOUT;
} catch ( Exception ex ) {

    state = PluginState.FAILED;
}

我希望在我的装载程序线程的调用类中我可以这样做:

LoaderThread t = new LoaderThread( pluginClassName );

// start loading the plugin
t.start();

// wait 5 seconds for startup
t.join( 5000 );

// interrupt if not complete
t.interrupt();

// get my plugin state
pluginState = t.getPluginState();

如果我错了,请告知,但

  

如何暂停对class.newInstance()的呼叫?

0 个答案:

没有答案