Executors.newSingleThreadExecutor

时间:2015-06-05 00:54:58

标签: java multithreading

我试图做一个Executors和Threads的简单例子。

当我致电newSingleThreadExecutor(new CustomThreadFactory)时,它会顺利进行,但当我使用null参数调用newSingleThreadExecutor(null)时,它会给我" AWT-EventQueue-0"显示java.lang.NullPointerException。

为什么?

private Executor creador = Executors.newSingleThreadExecutor(null); //at this point fails, and with no parameters everything goes well
private QRExplorer explorer; // this class implements Runnable
//...
public MainView()
{
   //...
   explorer = new QRExplorer();
}
private void pCamaraMouseClicked(java.awt.event.MouseEvent evt)
{                                     
      creador.execute(explorer); 
}

1 个答案:

答案 0 :(得分:3)

下面

Executors.newSingleThreadExecutor(null);

您尝试调用接受newSingleThreadExecutor参数的ThreadFactory重载。您将值null作为单个参数传递。

该方法的javadoc说明

  

抛出:

     
      
  • NullPointerException - 如果threadFactorynull
  •   

由于您正在传递null,因此调用将抛出NullPointerException