在Java中可以做这样的事情吗?我是在想。
首先,我只创建一个具有一个参数的新线程。
Thread thread = new Thread(new Person());
然后,在Person()的构造函数中,我想启动该线程。
那有可能这样吗?
public Person() {
// Here belongs some code for the constructor and then
// I would like to start the thread
}
最佳答案
不,你不能。在Java可以调用Thread()
构造函数之前,它首先必须热切评估所有参数,包括对Person()
构造函数的调用。这意味着在调用Person
构造函数时,外部Thread
对象甚至不存在或尚未初始化,因此您不能真正使用它。