- Threads are lightweight processes that can be executed concurrently within a process to improve responsiveness and resource utilization.
- Threads share the same memory as the process they belong to, making communication between threads cheaper than between processes.
- The main() method represents the initial thread when a Java program starts. Additional threads can be created by extending the Thread class or implementing the Runnable interface.
2.
Light weight process
Smallest unit of processing
Share some memory area of the process
Thread is executed inside the process
Executing multi threads simultaneously
WHAT IS THREAD
3.
WHAT IS MULTITHREADING
Multi processing Multi threading
Separate memory space Share same memory
Heavy weight Light weight process
Time and cost is more Less cost and time
4.
Threads are light weight process
Because they share same memory space for child
threads too. whereas process , do not allocate same
memory space.
Interthread communication is in expensive
Context switching of threads are in expensive
Process inside the interthread and context sensitive is
costly
Advantages of threads
5.
Threads are implemented in the form of objects that
contain a method is called run() method is the heart
and soul of any thread
public void run()
{
………. (statement for implementing thread)
……….
}
Creating threads
6.
The run() method should be invoked by an object of the
concerned thread.
This can be achieved by creating the thread the initiating it
with help of another thread method called start().
Following two ways
By creating a thread class: A class that extends thread class and
override its run() method with the code required method
Creating threads-con’d
7.
By converting a class to a thread: Define a class that
implements Runnable interface. The Runnable
interface has only one method , run() that is to be
defined in the method the code to be executed by the
thread
Creating threads-con’d
8. Declare the class as extending the thread class
Implement the run() method
Create a thread object and call the start() method to initiate the
thread execution
Declaring the class
the thread class can be extended as follows
Class MyThread extends Thread
{
………….
………….
………….
}
Extending the thread class
9.
Implementing the run() method:
The run() method has been inherited by the Mythread.
This method in order to implement the code to be executed by
our thread.
The basic implementation of run() like
public void run()
{
………
………
………
}
Extending the thread class
10.
Starting new thread:
to actually create and run an instance of our thread class
MyThread aThread = new MyThread();
aThread.start();
New objects of class MyThread, the thread will be
Newborn state.
The start() method to move into the runnable state
Extending the thread class
11. class MultithreadingDemo extends Thread{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
} }
Output:
My thread is in running state.
Example program
13. We create a thread object, the
thread is born and is said to be
in newborn state.
The thread is not yet scheduled
for running
Following things
Schedule it for running using start() method
Kill it using stop() method
Life cycle – New born state
14.
The running state means that the thread is ready for execution
and is waiting for the availability of the process
The thread has joined the queue of threads they are waiting for
execution
The process of assigning time to thread is called a time slicing
We can using the yield() method
Life cycle – Runnable state
15.
Running means the process has been time to the thread for
execution
Following situations
1. It has been suspended using suspend() method. A
suspended thread can be revived by using the resume()
method.
2. It has been made to sleep. We can a thread to sleep for a
specified time period using the method sleep(time).where
time is in milliseconds
Life cycle –Running state
16.
3. It has been hold to wait until some event occurs. This
is done using wait() method. The thread can be
scheduled to run again for the notify() method.
Running state-con’d
17.
A thread is said to be blocked when it is prevented
from entering into the runnable state and
subsequently the running state.
The thread is suspended, sleeping, or waiting in
order to satisfy certain requirement.
A blocked thread is “not runnable” but not dead it
has access to run again.
Life cycle-blocked state
18.
A running thread ends its life when it has completed
executing its run() method.
It is natural death
A thread can be killed as soon it is born , or while it
is running ,or even when it is “not runnable”
(blocked) condition.
Life cycle-Dead state
19.
It can be default running
When a program begins , one thread begins running
immediately
Importance of main threads
A thread from which other “child” thread can be created
Must be the threads to finish execution
Current Thread() returns reference to the current thread
It is public and static in nature thread objects
Main thread
20.
getName – get in thread name
getPriority – thread get priorities
isAlive – check if a thread is still running
Join – wait for a thread to terminated
run – entry point for the thread
sleep – suspend a thread for a period of time
start() – start a thread by calling its run method
Thread methods
21.
The call to sleep() method is enclosed in a try block
and followed by a catch block. This is necessary
because the sleep() method throws an exception. if
we fail to catch the exception, program will not
compile.
Syntax
Catch(threadDeath e) {
……..
……..
}
Thread execptions
23.
thread scheduler assigns processor to a thread based on
priority of thread.
Whenever we create a thread in Java, it always has some
priority assigned to it.
Java is set the priority is using method is setpriority()
method.
ThreadName.setPriority(intNumber);
The intNumber is an integer value
Thread priority
24.
public static int MIN_PRIORITY: This is minimum
priority that a thread can have. Value for this is 1.
public static int NORM_PRIORITY: This is default
priority of a thread if do not explicitly define it.
Value for this is 5
public static int MAX_PRIORITY: This is maximum
priority of a thread. Value for this is 10.
Priority-con’d
25. Capability to control the access of multiple threads to a
shared resource
Why?
To prevent the interference of threads
To prevent consistency problems.
Understanding locks
Every object has a lock or monitor.
A thread needs to acquire lock before accessing objects
Types
Process ,Thread
synchronization
26. Mutual extension Co-operation
Keeps threads from
interfering with one another
while sharing data
Synchronized method
Synchronized block
Static synchronization
Inter-thread communication
synchronized threads to
communicate with each
others
Thread synchronization
27.
We can create two ways
Using the extended Thread class
Implementing the Runnable interface
The Runnable interface declares the run()
Method that is required for implementing threads in
programs.
Runnable interface
28.
Following steps
Declare the class as implementing the Runnable
interface.
Implement the run() method.
Create a thread by defining an object that is
instantiated from “runnable” class as the target of
the thread
Call the thread start() method to run the thread.
Runnable interface
29. class MultithreadingDemo implements Runnable
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj=new Thread(obj);
tobj.start();
} }
Output:
My thread is in running state.
Runnable interface-example program