Thread states
Threads can be in one of the following states:
New. After the thread is instantiated, the thread is in the New state until the start()method is invoked. In this state, the thread is not considered alive.
Runnable. A thread comes into the runnable state when the start() method is invoked on it. It can also enter the runnable state from the running state or blocked state. The thread is considered alive when it is in this state.
Running. A thread moves from the runnable state into the running state when the thread scheduler chooses it to be the currently running thread.
Alive, but not runnable. A thread can be alive but not in a runnable state for a variety of reasons. It may be waiting, sleeping, or blocked.
Waiting. A thread is put into a waiting state by calling the wait() method. A call to notify() or notifyAll() may bring the thread from the waiting state into the runnable state. The sleep() method puts the thread into a sleeping state for a specified amount of time in milliseconds, as follows:
try {
Thread.sleep(3 * 60 * 1000); // thread sleeps for 3 minutes
} catch(InterruptedException ex){}
Blocked. A thread may enter a blocked state while waiting for a resource like I/O or the lock of another object. In this case, the thread moves into the runnable state when the resource becomes available.
Dead. A thread is considered dead when its run() method is completely executed. A dead thread can never enter any other state, not even if the start() method is invoked on it.
Important thread methods
The sleep() method causes the current thread to sleep for a given time in milliseconds:
public static void sleep(long millis) throws InterruptedException
The yield() method causes the current thread to move from the running state to the runnable state, so that other threads may get a chance to run. However, the next thread chosen for running might not be a different thread:
public static void yield()
The isAlive() method returns true if the thread upon which it is called has been started but not moved to the dead state:
public final boolean isAlive()
When a thread calls join() on another thread, the currently running thread will wait until the thread it joins with has completed:
void join()
void join(long millis)
void join(long millis, int nanos)
Thread synchronization
Every object in Java code has one lock, which is useful for ensuring that only one thread accesses critical code in the object at a time. This synchronization helps prevent the object's state from getting corrupted. If a thread has obtained the lock, no other thread can enter the synchronized code until the lock is released. When the thread holding the lock exits the synchronized code, the lock is released. Now another thread can get the object's lock and execute the synchronized code. If a thread tries to get the lock of an object when another thread has the lock, the thread goes into a blocked state until the lock is released.
Synchronized
You can use the synchronized keyword to declare a method as synchronized. This method cannot be accessed by multiple threads simultaneously. The synchronized keyword can also be used to mark a block of code as synchronized. For this, the argument passed should be the object whose lock you want to synchronize on. The syntax is shown below:
synchronized(obj)
{
// here add statements to be synchronized
}
// obj is the object being synchronized
wait(), notify(), and notifyAll()
The wait(), notify(), and notifyAll() methods are defined in the java.lang.Object class.
When the wait() method is invoked, a thread releases the lock on an object and moves from the running state to the waiting state. The notify() method is used to signal one of the threads waiting on the object to return to the runnable state. It is not possible to specify which of the waiting threads should be made runnable. The notifyAll() method causes all the waiting threads for an object to return to the runnable state.
A thread can invoke wait() or notify() on a particular object only if it currently holds the lock on that object. wait(), notify(), and notifyAll() should be called only from within the synchronized code.
No comments:
Post a Comment