Java多线程,主线程为什么停止了?

标签 java multithreading lwjgl

我试图为 LWJGL 的 Display.update() 创建一个单独的线程,但主线程正在 sleep 或在我的第二个线程运行时停止,如何使两个线程同时执行?

public Window(int width, int height, String title){

}

@Override
public void run() {
    try {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }   

    while(!Display.isCloseRequested()){
        Display.update();
        Main.Render();
    }

    Display.destroy();

}

}

public static void main(String[] args) {
    //To be moved
    Window window = new Window(100, 100, "1");
    Thread windowThread = new Thread(window);
    windowThread.run();

    System.out.print("I am workign, please show me!");
}

public static void Render(){

    System.out.println("Hello There!");

}

最佳答案

改变这个,

Thread windowThread = new Thread(window);
windowThread.run();

作为,

Thread windowThread = new Thread(window);
windowThread.start();

来自Here

The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.

The Thread class' run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run() method executes the run() method of the Runnable object in the new thread instead.

Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.

关于Java多线程,主线程为什么停止了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26443831/

相关文章:

java - Bean @Autowired 在 spring 中默认

java - 使用J48算法生成决策树

Java应用程序服务器执行堆栈线程亲和性

multithreading - 使用最大连接上限计算总下载时间

java - 如何避免在jsp中重新加载js和css

java - 模拟距某个点设定距离的 GPS 路径

c - 读写器访问多个读者

java - OpenGL 非常大的网格裁剪

java - 将eclipse项目导出到jar

java - OpenGL - 使用 GL11 的 OS X