Engineering Full Stack Apps with Java and JavaScript
Thread class has many important methods like init, start, stop, run, getName(), getPriority(), isAlive() and join(), and also few static methods like sleep(), yield() etc. Usually we override only the run() method and use the inherited versions of init, start etc
Invoking the start() method on a thread object executes the run() method as a new thread of execution. When you call the start method for a thread object, it will call a native code method that causes the OS to initiate another thread from which the run() method executes. We can call start method only once on a thread or it will throw IllegalStateException.
You can also call the run method directly, but if you call the run() method directly, it would simply operate like any other method and will run as part of the same thread that called it.
The final isAlive() method returns true if the thread is still running or the Thread has not terminated.
The final join() method waits until thread on which it is called is terminated. For example, thread1.join() suspends the current thread until thread1 dies.
You can also pass a long value to join method to specify the number of milliseconds you are prepared to wait for the death of a thread. For example, thread1.join(1000) wait upto 1 second for thread1 to die, and then continue execution.
The join() method can throw an InterruptedException if the current thread is interrupted by another thread.
Calling yield() will move the current thread from running to runnable, to give other threads a chance to execute. However the scheduler may still bring the same thread back to running. A better alternative according to Effective Java by Joshua Bloch is to use Thread.sleep(1) instead of Thread.yield().
Thread’s stop() method is deprecated and should not be used. Instead, you should use the interrupt mechanism.
A thread can signal another thread that it should stop executing by calling the interrupt() method for that thread. This doesn’t stop the thread, but sets a flag in the thread. This flag must be checked in the run() method to have any effect and the thread should then terminate itself.
The isInterrupted() method returns true if the interrupted flag has been set. This method does not reset the flag, but another static method interrupted() tests the flag for the currently executing thread and if it has been interrupted, it clears the interrupted flag in the current thread object and returns true.
When an InterruptedException is thrown, the flag that registers the interrupt is cleared, so a subsequent call to isInterrupted() or interrupted() returns false.
The static currentThread() method returns a reference to the thread in which it is called.
This static sleep() method causes the thread to suspend execution for a given time. The sleep method has two overloaded versions:
static void sleep (long milliseconds) throws InterruptedException
static void sleep (long milliseconds, int nanoseconds) throws InterruptedException
Thread.stop, Thread.suspend and Thread.resume are deprecated. You can find the reasons for deprecating them at:
http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
For more details on thread methods, you can refer to the java documentation for Thread class at:
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html