繁体   English   中英

主方法中的线程启动

[英]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++;
    }
}

您正在启动一个没有RunnableThread ,即。 使用Threadrun()实现,该实现为空。

您需要将类的实例传递给新的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM