<<< Creating threads | Index | Two ways to create a thread >>> |
Key methods of the Thread class, Runnable interface, and the Object class:
Thread.start() // JVM calls the run() method of the thread Runnable.run() // top-level entry into a thread Thread.run() Thread.sleep() // sleep for the specified number of milliseconds. // Calls from within a synchronized block - MANDATORY! Object.wait() // wait until someone calls notify() on this object Object.notify() // wake up objects that waits Object.notifyAll()
Common pitfalls:
calling run() instead of start()
calling Object's wait() notify() notifyAll() from an un-synchronized code. Note: IllegalMonitorStateException will be thrown.
calling Swing code directly from the main application startup thread. This must be done on Swing's event dispatch thread via SwingUtilities.invokeLater()
<<< Creating threads | Index | Two ways to create a thread >>> |