2_Multithreading
2_Multithreading
Multithreading
Multithreading in Java
• Multithreading in java is a process of executing multiple
threads simultaneously.
• Process-based Multitasking(Multiprocessing)
• Thread-based Multitasking(Multithreading)
1) Process-based Multitasking (Multiprocessing)
• New
• Runnable
• Running
• Non-Runnable (Blocked)
• Terminated (Dead)
States
• New : only Thread object is created in this state.It is in this
state before the start() invoked.
• Runnable/ready to run : life of thread starts from this state. It
enters into this first time by calling start( ) and several times
later.
• Running state: A thread is assigned a processor and is
running. The thread enters this state only after it is in the
Runnable state and scheduler assigns a processor to the
thread.
• Sleeping/waiting/blocked state: When sleep method is
invoked on a running thread it enteres the sleeping mode. It
returns to the runnable state when the sleep interval ends.
• Dead state: This is the last state of the thread. When run
method ends the thread is terminated. Once terminated a
thread cannot be terminated.
• How to create thread
• If you are not extending the Thread class,your class object would not be
treated as a thread object.So you need to explicitely create Thread
class object.We are passing the object of your class that implements
Runnable so that your class run() method may execute.(Example
UsingRunnable.java)
Starting a thread:
• The main thread can create several child threads and each thread will
run independently. A thread schedular manages the execution of
threads and assigns the CPU to each thread.
• Example MultipleThreadWithoutSleep.java
• Thread scheduler in java is the part of the JVM that decides which
thread should run.
• There is no guarantee that which runnable thread will be chosen to run
by the thread scheduler.
• Only one thread at a time can run in a single process.
• Thread priorities, load on the CPU, availability of resources and type of
scheduling decides which thread should execute.
• The thread scheduler mainly uses preemptive or time slicing scheduling
to schedule the thread
• Difference between preemptive and time slicing
– Under preemptive scheduling, the highest priority task executes until
it enters the waiting or dead states or a higher priority task comes
into existence. Under time slicing, a task executes for a predefined
slice of time and then reenters the pool of ready tasks. The
scheduler then determines which task should execute next, based
on priority and other factors.
Sleep method in java
• In java sleep() method of Thread class is used to sleep a thread for the
specified milliseconds of time.
• We can pause a running thread using a sleep method.
• This method throws an interruptedException when it is interrupted by
another thread.
• Example MultipleThreadWithSleep.java
• In our program we have given a delay of 2 seconds between
messages. So when a thread is paused the other thread will get control
and execute.
Can we start a thread twice?
• No. After staring a thread, it can never be started again. If you does so,
an IllegalThreadStateException is thrown.
• For Example: TwiceThread.java
What if we call run() method directly instead start() method?