Threads
Threads
Thread:
2. Multi-threading:
- At this point, the thread is not yet scheduled for execution and it’s a state internal to Java programming.
2. Runnable:
- A thread enters the "Runnable" state after invoking the start() method.
- In this state, the Thread is eligible to be scheduled by the operating system and can start executing its
code.
- Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the
OS implementation of thread scheduler
3. Running
- When thread is executing, it’s state is changed to Running.
- Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to
Running.
4. Blocked/Waiting:
- A thread can enter the "Blocked" or "Waiting" state under certain circumstances.
- If a thread is waiting for a lock to be released by another thread, it goes into the "Blocked" state.
- Similarly, if a thread waits for a specific condition to be satisfied, it enters the "Waiting" state.
- In these states, the Thread is not actively executing its code and is not eligible for scheduling.
5. Timed Waiting:
- A thread can enter into the "Timed Waiting" state when they are paused for a specified amount of time.
This can happen when using. This can occur if it calls methods sleep() or wait() with a specific timeout
value.
- The Thread remains in this specific state until the timeout expires or until it receives a notification from
another thread.
6. Terminated:
- The final state in the thread lifecycle is the "Terminated" state.
- A thread enters this state when it completes its execution or when an unhandled exception occurs
within the Thread.
join(long millis) Waits at most millis milliseconds for this thread to die
If this thread was constructed using a separate Runnable run object, then that
run() Runnable object’s run method is called; otherwise, this method does nothing and
returns
setPriority(int
Changes the priority of this thread
newPriority)
Causes the currently executing thread to sleep (temporarily cease execution) for
sleep(long millis) the specified number of milliseconds, subject to the precision and accuracy of
system timers and schedulers
Causes this thread to begin execution; the Java Virtual Machine calls the run
start()
method of this thread
A hint to the scheduler that the current thread is willing to yield its current use of a
yield()
processor
Object wait methods has three variance, one which waits indefinitely for any other
thread to call notify or notifyAll method on the object to wake up the current
Wait()
thread. Other two variances puts the current thread in wait for specific amount of
time before they wake up.
notify method wakes up only one thread waiting on the object and that thread
notify () starts execution. So if there are multiple threads waiting for an object, this method
will wake up only one of them. The choice of the thread to wake depends on the
Methods Action Performed
wakes up all the threads waiting on the object, although which one will process
first depends on the OS implementation.
notifyAll() These methods can be used to implement producer consumer problem where
consumer threads are waiting for the objects in Queue and producer threads put
object in queue and notify the waiting threads.
7. Synchronization
Synchronized method.
Synchronized block.
Static synchroniz ation.
8. DeadLock
- a situation where two or more threads are blocked forever, waiting for each other