0

I am trying to create a separate thread for LWJGL's Display.update(), but the main thread is sleeping or is stopped while my second thread is running, how can I make both threads execute simultaneously?

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!");

}
user2395615
  • 15
  • 1
  • 5
  • 2
    Don't call `run` - it *doesn't* create a new thread. Call `start`. (So much confusion could have been saved if calling it just about anything other than `run`..) – user2864740 Oct 18 '14 at 19:46
  • @user2864740, that's a bad place to redirect this topic because it doesn't _directly_ answer the question. The question was, "why doesn't this work?", and the direct answer is what Rakesh KR said, below. – Solomon Slow Oct 18 '14 at 21:01
  • @jameslarge I don't find it "bad", in particular because the high-voted answers (except for the accepted answer, unfortunately) answer cover the problem. If you have a better duplicate (*because there are many*), feel free to choose it instead. – user2864740 Oct 18 '14 at 21:38
  • @jameslarge http://stackoverflow.com/questions/2674174 , http://stackoverflow.com/questions/4830302 , http://stackoverflow.com/questions/15841301/ , http://stackoverflow.com/questions/25094229 , http://stackoverflow.com/questions/25586271 , http://stackoverflow.com/questions/23907118 – user2864740 Oct 18 '14 at 21:41

1 Answers1

1

Change this,

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

as,

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

From 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.

Community
  • 1
  • 1
Rakesh KR
  • 6,049
  • 5
  • 39
  • 49
  • Re "The start() method starts the execution of the new thread and calls the run() method." No! The start() method does _not_ call the run() method. The start() method creates a new thread and returns. The run() method is called by something (I don't know what, and most of us don't need to know) _in_ the new thread. – Solomon Slow Oct 18 '14 at 21:03
  • Besides the technical error, that text that you quoted is confusing. Here's a simpler explanation: The Thread.start() method is provided by the library for your code to call to start a new thread. The run() method is provided by your code for the library to call in the new thread. – Solomon Slow Oct 18 '14 at 21:07
  • Also, extending Thread class is hackish, and limiting. Hackish becaue you shouldn't extend `Thread` unless you want to make a new _kind_ of `Thread`. (e.g., a thread that does some special bookkeeping in its `interrupt()` method.) It's limiting because Java only has single inheritance: If your class extends Thread, then it can't extend anything else, but it _can_ extend something else if it `implements Runnable`. – Solomon Slow Oct 18 '14 at 21:14