【问题标题】:Thread start in main method在 main 方法中启动线程
【发布时间】:2013-10-03 21:02:37
【问题描述】:

我试图在 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++;
    }
}

【问题讨论】:

    标签: java multithreading main


    【解决方案1】:

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

    您需要将您的类的一个实例传递给新的Thread 对象的构造函数。

    public static void main(String args[]){
        Thread thread = new Thread(new theForestThread());
        thread.start();
    }
    

    【讨论】:

      【解决方案2】:

      尝试下一个:

      new Thread(new(theForestThread())).start();
      

      http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html中查看更多信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        相关资源
        最近更新 更多