![](/img/trans.png)
[英]Is it redundant to start a thread in the main method if that's all you're going to do in the main method?
[英]Thread start in main method
我正在尝试在main方法内部启动线程,但是当我启动线程时,它将不会调用run方法。 我认为这可能与在线程中启动线程有关:
package com.audiack.theForest;
public class theForestThread implements Runnable {
private static int theBeginningTimes = 0;
private static TheBeginning theBeginning = new TheBeginning();
public static void main(String args[]){
Thread thread = new Thread();
thread.start();
}
@Override
public void run() {
theBeginning.start(theBeginningTimes);
theBeginningTimes++;
}
}
您正在启动一个没有Runnable
的Thread
,即。 使用Thread
的run()
实现,该实现为空。
您需要将类的实例传递给新的Thread
对象的构造函数。
public static void main(String args[]){
Thread thread = new Thread(new theForestThread());
thread.start();
}
尝试下一个:
new Thread(new(theForestThread())).start();
请参阅http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html中的更多内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.