Skip to content

Instantly share code, notes, and snippets.

@pavan538
Created February 7, 2018 06:27
Show Gist options
  • Save pavan538/bf1561f86d01b8f5ad1d97c2d865f7fc to your computer and use it in GitHub Desktop.
Save pavan538/bf1561f86d01b8f5ad1d97c2d865f7fc to your computer and use it in GitHub Desktop.
Threads Notes
Threads:
Thread is a small piece of code executed separately.
Threads are created with three pieces of information
Thread name
Runnable target - run()
stack size
Life cycle of a Thread:
By using Thread.getState() - we can know the state of the thread.
Thread.getState() returns enum state of a thread.
New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when thread is in this state. When a thread lies in the new state, it’s code is yet to be run and hasn’t started to execute.
Runnable State: A thread that is ready to run is moved to runnable state. In this state, a thread might actually be running or it might be ready run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread. Each and every thread runs for a short while and then pauses and relinquishes the CPU to another thread, so that other threads can get a chance to run. When this happens, all such threads that are ready to run, waiting for the CPU and the currently running thread lies in runnable state.
Blocked/Waiting state:When a thread is temporarily inactive, then it’s in one of the following states:
Blocked
Waiting
For example, when a thread is waiting for I/O to complete, it lies in the blocked state. It’s the responsibility of the thread scheduler to reactivate and schedule a blocked/waiting thread. A thread in this state cannot continue its execution any further until it is moved to runnable state. Any thread in these states do not consume any CPU cycle.
A thread is in the blocked state when it tries to access a protected section of code that is currently locked by some other thread. When the protected section is unlocked, the schedule picks one of the thread which is blocked for that section and moves it to the runnable state. Whereas, a thread is in the waiting state when it waits for another thread on a condition. When this condition is fulfilled, the scheduler is notified and the waiting thread is moved to runnable state.
If a currently running thread is moved to blocked/waiting state, another thread in the runnable state is scheduled by the thread scheduler to run. It is the responsibility of thread scheduler to determine which thread to run.
Timed Waiting: A thread lies in timed waiting state when it calls a method with a time out parameter. A thread lies in this state until the timeout is completed or until a notification is received. For example, when a thread calls sleep or a conditional wait, it is moved to timed waiting state.
Terminated State: A thread terminates because of either of the following reasons:
Because it exists normally. This happens when the code of thread has entirely executed by the program.
Because there occurred some unusual erroneous event, like segmentation fault or an unhandled exception.
A thread that lies in terminated state does no longer consumes any cycles of CPU.
Implementing Thread States in Java:
In Java, to get the current state of the thread, use Thread.getState() method to get the current state of the thread. Java provides java.lang.Thread.State class that defines the ENUM constants for the state of a thread, as summary of which is given below:
Constant type: New
Declaration: public static final Thread.State NEW
Description: Thread state for a thread which has not yet started.
Constant type: Runnable
Declaration: public static final Thread.State RUNNABLE
Description: Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
Constant type: Blocked
Declaration: public static final Thread.State BLOCKED
Description: Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait().
Constant type: Waiting
Declaration: public static final Thread.State WAITING
Description: Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
Object.wait with no timeout
Thread.join with no timeout
LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action.
Constant type: Timed Waiting
Declaration: public static final Thread.State TIMED_WAITING
Description: Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
Thread.sleep
Object.wait with timeout
Thread.join with timeout
LockSupport.parkNanos
LockSupport.parkUntil
Constant type: Terminated
Declaration: public static final Thread.State TERMINATED
Description: Thread state for a terminated thread. The thread has completed execution.
Difference between Waiting and Blocked states:
A thread goes to wait state once it calls wait() on an Object.
This is called Waiting State. Once a thread reaches waiting state,
it will need to wait till some other thread notify() or notifyAll() on the object.
Once this thread is notified, it will not be runnable.
It might be that other threads are also notified(using notifyAll()) or
the first thread has not finished his work, so it is still blocked till it gets
its chance. This is called Blocked State.
Once other threads have left and its this thread chance, it moves to Runnable
state after that it is eligible pick up work based on JVM threading mechanism and
moves to run state.
Creating threads:
We can create threads in the following ways
a) By implementing Runnable interface for a class (or)
passing implementing Runnable interface anonymously in Thread class constructor.
b) By extending Thread class
Difference between extending Thread and implementing Runnable:
a) By extending Thread class, we cannot extend other classes, because java doesnt support
multiple inheritance.
b) By implementing Runnable interface, we are creating a new Runnable class with different
behaviour job. It give us freedom to use specific behaviour job when required.
c) extends Thread means we are inheriting all functions and variables to child class.
where as with Runnable interface we can specify the job to be done.
volatile keyword in java:
when ever there is a static shared variable for class and if two threads are working on
that shared variable in different processor, changes made in one thread won't be reflected to other thread
immediately.
Note that write of normal variables without any synchronization actions, might not be visible
to any reading thread (this behavior is called sequential consistency). Although most modern
hardware provide good cache coherence therefore most probably the changes in one cache are
reflected in other but it’s not a good practice to rely on hardware for to ‘fix’ a faulty
application.
To prevent the above, we declare shared variable as volatile which is stored in main memory.
so changes made by one thread will be automatically reflected to another thread.
Mutex:
every object in java has an intrinsic lock or mutex.
in the case of multiple threads, only one thread can acquire the intrinsic lock of an object.
Synchronization in java:
In java Synchronization can be done in two ways:
object level lock:
if we use synchronized on object method or on object and or on block, only one thread can get the lock,
and execute the block or method.
class level lock:
Every lock in java has a unique lock which is nothing but class level lock.
If a thread wants to execute a static synchronized method, then thread requires class
level lock. Once a thread got the class level lock, then it is allowed to execute any
static synchronized method of that class. Once method execution completes automatically
thread releases the lock.
use of synchronized in various aspects:
Synchronized Instance Methods - public synchronized void add(int a, int b)
Synchronized Static Methods - public static synchronized void add(int a, int b)
Synchronized Blocks in Instance Methods - synchronized(this)
Synchronized Blocks in Static Methods - synchronized(Example.class)
ArrayBlockingQueue:
put() - will wait if the queue is full, when called by producer.
take() - will wait if the queue is empty, when called by consumer.
wait(): - It is always called in synchronized block.
synchronized(this) {
sout("i will be waiting");
wait(); // wait will release the intrinsic lock from the block
sout('i am notified ');
}
notify():
synchronized(this) {
sout("i am executing")
notify(); // notify will notify the other thread which is waiting for intrinsic lock
// notify() - execute entire block of synchronized block and then releases the lock.
Thread.sleep(5000)
sout("I am done with my block")
}
################## Action Items #############################
check wait() and notify() method also works of Lock objects(Reentrant Locks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment