我有一个本质上是Singelton的类,并实现了Callable接口。可以预期的是,一旦实例化,我应该立即启动该单例类的线程。尝试在构造函数,静态块,构造函数块中执行此操作,但是我得到了ExceptionInInitializerError
,因为我认为该对象直到那个时候才被实例化。谁能指导我该怎么做?
类结构在下面
interface X implements Callable<Object>{
void metho1();
}
class A implements X{
private Map<String, String> map = new HashMap<>();
private ExecutorService executor = Executors.newFixedThreadPool(1);
private static A a = new A();
public static A getA(){
return a;
}
private A()
{
//Obiously will not work because by the time I reach here 'a' is not instantiataed
executor.submit(getA());
}
static{
//This also does not work
executor.submit(getA());
}
@Override
public Object call()
{
//Do something
}
@Override
public void metho1(){
//Do something
}
}