Multithreading
in
Java
1rkm
Multithreading in Java
You will Learn :
 Multiprogramming
 Multitasking
 Multithreading & Thread
 Diff between Multiprogramming & Multithreading
 Practical use of Multithreading
 Advantages
 Implementation of Multithreading in Java
 Built-in Library for Multithreading in Java
 Important methods to implement Multithreading in Java
 Program of Multithreading in Eclipse
2rkm
Multiprogramming
Multiprogramming is the ability of an operating system to execute
multiple programs at the same time on single processor machine.
If the currently executing process performs i/o operation or
waiting for i/o then the Operating System may interrupt that
process and gives the control to other process residing in main
memory.
Process 1
Process 2
i/o of P1
3rkm
The process of having a computer perform multiple tasks
simultaneously.
For example : Listening of music in background while browsing
internet or typing in MS-Word in foreground.
Multitasking
4rkm
Multithreading
It is the ability of a CPU which execute multiple
thread concurrently, supported by the operating system
Thread : A Thread is the smallest
sequence of programmed
instructions that can be managed
independently.
Threads are independent because
they all have separate path of
execution that’s the reason if an
exception occurs in one thread, it
doesn’t affect the execution of
other threads.
5rkm
Objective of Multithreading:
•To increase utilization of a single core by using Thread-level parallelism, as well
as Instruction-level parallelism.
Difference between Process and Thread :
•Process means any program is in execution while thread is a segment of the process.
•Every process uses separate memory while all the threads use shared memory.
Multithreading
6rkm
While you are typing on Ms-Word spell check checking spelling
and informed to user about the spelling mistake. Spell check
working as a Thread.
Practical use of Thread
7rkm
User
•When you insert ATM card into ATM Machine it start a new thread to
perform your request. There are multiple account holders on the same
server requesting the server simultaneously. Server software create a
new thread for every user.
•Railway reservation system where multiple users accessing the same
server.
Practical use of Thread
Server
8rkm
•When you request to Amazon server from your mobile
through Amazon App, that request become a thread and
your Mobile Application doing another task when request
is forward in the form of thread.
Practical use of Thread
9rkm
In multimedia games you can use multiple objects like
human, gun etc. These objects are nothing but the
threads.
Practical use of Thread
10rkm
Advantages of Multithreading
•Threads are independent so users are not blocked and we
can perform multiple operations at times.
•The other threads won't get affected if one thread meets an
exception.
•Multiple threads don't allocate separate memory area, hence they
save memory.
•In Multithreading context switching between threads takes less
time.
11rkm
Implementation of Threads in Java
Thread Life Cycle in Java
12rkm
•New: The thread is created using class "Thread class". It remains in this
state till the program starts the thread. It is also known as born thread.
•Runnable: The instance of the thread is invoked with a start method.
The thread control is given to scheduler to finish the execution. It
depends on the scheduler, whether to run the thread.
•Running: The scheduler selects one thread from the thread pool, and it
starts executing in the application.
•Waiting: This is the state when a thread has to wait. As there multiple
threads are running in the application, there is a need for synchronization
between threads. Hence, one thread has to wait, till the other thread gets
executed. Therefore, this state is referred as waiting state.
•Dead: This is the state when the thread is terminated. The thread is in
running state and as soon as it completed processing it is in "dead
state".
Thread Life Cycle in Java (Stages)
13rkm
Built-in Library of Java for Thread
Built-in Class : Thread
Built-in Interface : Runnable
Package for both : java.lang
(java.lang is default package of Java so it
is no need to import explicitly. )
14rkm
Important Methods of Java Thread Class
void start() : It use to start the execution of thread. It call the run()
method defined to do some action in thread.
void run() : It used to define an action for thread.
static void sleep() : It sleeps a thread for specified time or a thread enter in
wait state for a specified time.
void stop(): It used to stop a thread.
void destroy() : It used to destroy the thread.
void suspend() : It used to suspend the thread.
void resume() : It resume the suspended thread.
static yield(): It pause the currently executing thread and allow other
thread to execute temporarily.
15rkm
Practical Program on Eclipse
16rkm

Introduction to Multithreading in Java

  • 1.
  • 2.
    Multithreading in Java Youwill Learn :  Multiprogramming  Multitasking  Multithreading & Thread  Diff between Multiprogramming & Multithreading  Practical use of Multithreading  Advantages  Implementation of Multithreading in Java  Built-in Library for Multithreading in Java  Important methods to implement Multithreading in Java  Program of Multithreading in Eclipse 2rkm
  • 3.
    Multiprogramming Multiprogramming is theability of an operating system to execute multiple programs at the same time on single processor machine. If the currently executing process performs i/o operation or waiting for i/o then the Operating System may interrupt that process and gives the control to other process residing in main memory. Process 1 Process 2 i/o of P1 3rkm
  • 4.
    The process ofhaving a computer perform multiple tasks simultaneously. For example : Listening of music in background while browsing internet or typing in MS-Word in foreground. Multitasking 4rkm
  • 5.
    Multithreading It is theability of a CPU which execute multiple thread concurrently, supported by the operating system Thread : A Thread is the smallest sequence of programmed instructions that can be managed independently. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads. 5rkm
  • 6.
    Objective of Multithreading: •Toincrease utilization of a single core by using Thread-level parallelism, as well as Instruction-level parallelism. Difference between Process and Thread : •Process means any program is in execution while thread is a segment of the process. •Every process uses separate memory while all the threads use shared memory. Multithreading 6rkm
  • 7.
    While you aretyping on Ms-Word spell check checking spelling and informed to user about the spelling mistake. Spell check working as a Thread. Practical use of Thread 7rkm
  • 8.
    User •When you insertATM card into ATM Machine it start a new thread to perform your request. There are multiple account holders on the same server requesting the server simultaneously. Server software create a new thread for every user. •Railway reservation system where multiple users accessing the same server. Practical use of Thread Server 8rkm
  • 9.
    •When you requestto Amazon server from your mobile through Amazon App, that request become a thread and your Mobile Application doing another task when request is forward in the form of thread. Practical use of Thread 9rkm
  • 10.
    In multimedia gamesyou can use multiple objects like human, gun etc. These objects are nothing but the threads. Practical use of Thread 10rkm
  • 11.
    Advantages of Multithreading •Threadsare independent so users are not blocked and we can perform multiple operations at times. •The other threads won't get affected if one thread meets an exception. •Multiple threads don't allocate separate memory area, hence they save memory. •In Multithreading context switching between threads takes less time. 11rkm
  • 12.
    Implementation of Threadsin Java Thread Life Cycle in Java 12rkm
  • 13.
    •New: The threadis created using class "Thread class". It remains in this state till the program starts the thread. It is also known as born thread. •Runnable: The instance of the thread is invoked with a start method. The thread control is given to scheduler to finish the execution. It depends on the scheduler, whether to run the thread. •Running: The scheduler selects one thread from the thread pool, and it starts executing in the application. •Waiting: This is the state when a thread has to wait. As there multiple threads are running in the application, there is a need for synchronization between threads. Hence, one thread has to wait, till the other thread gets executed. Therefore, this state is referred as waiting state. •Dead: This is the state when the thread is terminated. The thread is in running state and as soon as it completed processing it is in "dead state". Thread Life Cycle in Java (Stages) 13rkm
  • 14.
    Built-in Library ofJava for Thread Built-in Class : Thread Built-in Interface : Runnable Package for both : java.lang (java.lang is default package of Java so it is no need to import explicitly. ) 14rkm
  • 15.
    Important Methods ofJava Thread Class void start() : It use to start the execution of thread. It call the run() method defined to do some action in thread. void run() : It used to define an action for thread. static void sleep() : It sleeps a thread for specified time or a thread enter in wait state for a specified time. void stop(): It used to stop a thread. void destroy() : It used to destroy the thread. void suspend() : It used to suspend the thread. void resume() : It resume the suspended thread. static yield(): It pause the currently executing thread and allow other thread to execute temporarily. 15rkm
  • 16.
    Practical Program onEclipse 16rkm