Professional Documents
Culture Documents
Multithreading
Multithreading
Multithreading
• Multithreading in java is a process of executing
multiple threads simultaneously.
• A Multi threaded program contains two or more parts
that can run concurrently.
• Each part of a program is called a thread.
• Multi threading is a specialized form of multi tasking.
Multitasking Fundamentals
• Multitasking is a process of executing multiple tasks
simultaneously.
• We use multitasking to utilize the CPU.
Multitasking can be achieved by two ways:
• Process-based Multitasking(Multiprocessing)
• Thread-based Multitasking(Multithreading)
Multi Threading Fundamentals
Process based multi tasking:
•A feature that allows to execute two or more programs concurrently.
•Here a program is the smallest unit of code that can be dispatched
by the scheduler.
Eg: running the java compiler, using text editor or browsing the
internet simultaneously…
Thread based Multitasking:
•Here the thread is Smallest unit of dispatchable code.
• single program can perform two or more tasks at once.
Eg: a text editor can be formatting text at the same time that
it is printing, as long as these two actions are being performed by
two separate threads.
Process-based vs. Thread-based
1. A process is a program that is 1. A thread is a separate path of
executing. execution.
2. Processes are heavy weight tasks 2. Threads are light weight process.
that require their own separate They share the same address space
address spaces.
3. Interprocess communication is 3. Inter thread communication is
expensive and limited. inexpensive.
We can override the other methods of We can only implement only run
thread also along with run(). method.
We can not inherit other classes. We can inherit other classes also.
Methods in Thread class
• currentThread() method returns a reference
to the currently executing thread object.
• getName() method returns this thread's name.
Method Description
setName() to give thread a name
getName() return thread's name
getPriority() return thread's priority
isAlive() checks if thread is still running or
not
join() Wait for a thread to end
run() Entry point for a thread
sleep() suspend thread for a specified
time
start() start a thread by calling run()
method
Constructors of Thread class
• Thread ( )
• Thread ( String str )
• Thread ( Runnable r )
Thread Class Constructors
1. Thread() Allocates a new Thread Object.
E.g., Thread t=new Thread();
3. Thread(Runnable target)
E.g: SecondThread st= new SecondThread();
Thread t=new Thread(st);
}
public static void main(String args[])
{
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.start();
m2.start();
}
}
Synchronization
• When two or more threads need access to a shared resource, they need
some way to ensure that the resource will be used by only one thread at
a time.
• Synchronization used to solve data inconsistency.
• Synchronization is a mechanism to ensure that a resource is shared by
one thread at a time.
Eg: When one thread is writing to a file, a second thread must be
prevented from doing so at the same time
• Key to synchronization in java is the concept of monitor.
• A monitor is an object that is used as a lock. Only one thread can own
a monitor at a given time.
• All objects in java have a monitor object by default. Thus all object
supports synchronization.
• When a thread enters into the monitor all other threads attempting to
enter the locked monitor will be suspended until the first thread exits
the monitor.
Synchronization
•If we do not use syncronization, and let two or more threads access a
shared resource at the same time, it will lead to distorted results.