yield() to temporarily give it up.The user presses a button on the GUI interface that starts a long action. The single GUI thread is now in use, so the GUI interface becomes completely unresponsive until the action is completed.
For long actions, start a separate thread so that the GUI thread may continue to operate the interface.
IO is relatively slow. Let's say you're writing a browser. It reads a web page that has a lot of links to images. If you get the images sequentially, the program will spend most of its time waiting for network IO operations to complete.
Start a separate thread for each image that must be loaded. The IO wait time is therefore reduced to the longest wait, not the sum of all waits.
You want to show an animated image, but also continue regular processing. Every so many milliseconds you need to draw a new image. How can you do this while you are doing some other computation?
Start a separate thread for the animation that starts up at regular intervals. The javax.swing.Timer class is a class that helps you do this.
notify() / notifyAll())run() method.start() a tread.th.setDaemon(true);Thread.currentThread() - Returns current thread.Thread.sleep(millis) - Sleeps current thread # milliseconds.th.start() - Starts thread th.th.interrupt() - Interrupts th.
th.isInterrupted() - True if interrupted. Eg,
if (Thread.currentThread().isInterrupted()) ...th.join() - Waits for th to "die".th.yield() - Voluntarily allows other threads to execute.
Thread busyBee = new Thread() {
public void run() {
doAllThatWork();
}
}
busyBee.start();
obj.wait() - Current thread waits until object unlocked by notify() call.obj.notify() - Awakens arbitrary thread "waiting" for this object.
Can only be called if current thread owns the lock on this
object (eg, in a synchronized method). Arbitrary thread
is still blocked until current thread releases lock.obj.notifyAll() - Awakens all threads "waiting" for this object..setVisible().