Threads
Threads
Multithreading
Life cycle
Dead Lock
Synchronization
Methods of Threads
Threads communication
Threads
•Thread is a single execution sequence within the process.
•A thread is sometimes called a lightweight process.
Multithreading
A multithreaded program contain two or more parts that can run
concurrently. Each part of such a program is called thread and each
thread defines a separate path of execution.
Difference between processes and threads?
A process is an execution of a program but a thread is a single
execution sequence within the process. A process can contain multiple
threads. A thread is sometimes called a lightweight process.
Different ways of creating a thread
1)Extending the Thread class
class Counter extends Thread {
public void run(){
[Link](“Thread is running”);
}
public static void main(String[] args){
Thread t1 = new Counter();
[Link]();
}}
2)Implementing the Runnable interface.
class Counter extends Base implements Runnable {
public void run(){
//logic to execute in a thread
}
public static void main(String[] args){
Thread t1 = new Thread(new Counter());
[Link]();
}}
Different states of a thread are :
Life cycle of Thread
New state – After the creations of Thread instance the thread is in this state
but before the start() method invocation. At this point, the thread is
considered not alive.
Runnable (Ready-to-run) state – A thread start its life from Runnable state.
A thread first enters runnable state after the invoking of start() method but a
thread can return to this state after either running, waiting, sleeping or
coming back from blocked state also. On this state a thread is waiting for a
turn on the processor.
Running state – A thread is in running state that means the thread is
currently executing. There are several ways to enter in Runnable state but
there is only one way to enter in Running state: the scheduler select a
thread from runnable pool.
Dead state – A thread can be considered dead when its run() method
completes. If any thread comes on this state that means it cannot ever run
again.
Blocked - A thread can enter in this state because of waiting the resources
that are hold by another thread
Synchronization
Synchronization is the capability to control the access of multiple threads to
share resources. Without synchronization it is possible for one thread to
modify a shared object while another thread is in the process of using or
updating that object's [Link] often leads to an error.
The synchronized keyword can be applied in method level or block level of code
Method level Example for Synchronization
public synchronized void enqueue(Object item)
{
// Body of method goes here
}
BlockLevel Example for Synchronization
public void enqueue(Object item)
{
synchronized (this)
{
//Body of method goes here
}
}
Thread deadlock
Deadlock occurs when two threads are blocked, witheach waiting for
others lock.
Methods from the [Link] class
Public static void sleep(long millis)throws InterruptedException
Public static void yield()
Public final void join()throws InterruptedException
Public final void setPriority(int newPriority)
Sleep()
[Link] causes the current thread to suspend execution for a
specified period. This is an efficient means of making processor time
available to the other threads of an application or other applications that
might be running on a computer system.
Yield():
Causes the currently executing thread object to temporarily pause and
allow other threads to execute.
What is the difference between yield and sleep()?
When a task invokes yield(), it changes from running state to runnable
state. When a task invokes sleep(), it changes from running state to
waiting/sleeping state.
Join()
The method that you will more commonly use to wait for a thread to finish is
called join( )
Setting thread priorities
Setting a threads priority can be very useful if one thread has more critical
tasks to perform than [Link] Thread class has a method called
setPriority(int level) with which you can alter the priority a Thread instance
[Link] priority level range from 1 (least important) to 10 (most important)
and if no level is explicitly set, a Thread instance has the priority level of 5.
Threads communicate with each other
The wait(), notify(), and notifyAll() methods are used to provide an efficient
way for threads to communicate with each other. This communication
solves the ‘consumer-producer problem’. This problem occurs when the
producer thread is completing work that the other thread (consumer thread)
will use.
the wait() method causes a thread to release the lock it is holding on an
object; allowing another thread to run
notify() wakes up a single thread which is waiting on the object's lock
notifyAll() wakes up ALL waiting threads; the scheduler decides which one
will run