0% found this document useful (0 votes)
29 views15 pages

UNIT3 Part2 (Multithreading)

The document discusses the basics of multithreading in Java. It defines a process as a program that is executing and a thread as a lightweight sub-process that shares resources with other threads within the same process. Multithreading allows a program to split into multiple threads that can run concurrently, enabling more efficient use of the CPU than single-threaded programs. The document outlines the key concepts of processes, threads, multitasking, multithreading, and the lifecycle of a thread in Java. It also discusses features such as thread priorities, synchronization, and messaging that enable communication between threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views15 pages

UNIT3 Part2 (Multithreading)

The document discusses the basics of multithreading in Java. It defines a process as a program that is executing and a thread as a lightweight sub-process that shares resources with other threads within the same process. Multithreading allows a program to split into multiple threads that can run concurrently, enabling more efficient use of the CPU than single-threaded programs. The document outlines the key concepts of processes, threads, multitasking, multithreading, and the lifecycle of a thread in Java. It also discusses features such as thread priorities, synchronization, and messaging that enable communication between threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

MultiThreading

Unit-3
Basics
• Process
– A program that is executing.
• Thread
– Similar to a program that has single flow of control.
– It has beginning, body, and end.
• Multitasking
– Executing several programs simultaneously.
• Multithreading
– Dividing program into 2 or more subprograms, which can be
implemented at same time(in parallel).
• Process based multitasking
– Allowing computer to run 2 or more programs concurrently.
• Thread based multitasking
– Single program can perform 2 or more tasks simultaneously.
Process
• A program that is executing.
• Process based multitasking is a feature that
allows your computer to run 2 or more programs
concurrently.
• Processes are heavyweight tasks that require
their own separate address spaces.
• Disadvantages:
– Inter rocess communication is expensive and limited.
– Context switching from one process to another is
costly.
Thread
• It is a small unit.
• Similar to a program that has single flow of control.
• It has beginning, body, and end.
• Threads are lightweight.
• They share the same address space and cooperatively
share the same heavy weight process.
• Inter thread communication is inexpensive and context
switching from one thread to other is low cost.
• Multithreading enables you to write very efficient
programs that make maximum use of the CPU.
Thread

Drawback: event
loop with polling

NOTE: unique property of Java is , it supports Multithreading.


Life Cycle of a Thread

5 States:
- Newborn
- Runnable
- Running
- Blocked
- Dead
Features of Multithreading
• Thread Priorities
• Synchronization
• Messaging
Thread Priorities
• Integers (least number means min priority)
• Determines how thread should be treated
w.r.t other threads.
• It is used to decide when to switch from one
running thread to the next.
• Threads with same priority will follow round
robin fashion.
Synchronization
• Used to overcome asynchronous behaviour.
• Monitor:
– It is a inter process synchronization mechanism.
– Once a thread is inside a sync. Method,
no other thread can call any other
sync. Method on the same object.
Messaging
• Java provides a clean, low cost way for 2 or
more threads to talk to each other, via calls to
predefined methods that all objects have.
• Java’s messaging system allows a thread to
enter a sync. Method on an object, and then
wait there until some other thread explicitly
notifies it to come out.
Creating Thread
• There are 2 ways. Using –
– Thread class ( from java.lang package)
– Runnable interface ( from java.lang package)
Using Thread class
• Methods in Thread class:
– Run(): entry point for the thread
– Start(): to start any thread ( it calls run method in it)
– Stop(): to stop the thread
– Sleep(): to block the thread at specified time
– Suspend(): block the thread until further orders
– Resume(): to invoke suspended threads
– Wait(): block the thread until certain condition occurs
– Notify(): to invoke wait threads
– getName(): to obtain threads name
– getPriority(): to obtain threads priority
– isAlive(): to determine thread is alive or not
Using Thread class
• Note: The main thread is a thread that all of the
other threads have been using.
• Steps:
– Import java.lang package
– Declare a class extending Thread class
– Override run() method that is responsible for
executing the sequence of code that the thread will
execute.
– Create a thread object and call the start() method to
initiate the thread execution.
Using Runnable Interface
• Runnable interface defines only one method
called run().
• Run() method can call other methods, use
other classes, and declare variables just like
main thread.
• Start() method causes the JVM to call run()
method.
• Example program.
Thread Priorities
• Thread Priorities are used by the thread scheduler to
decide when each thread should be allowed to run.
• To set a thread priority, use setPriority() method.
– Syntax: void setPriority(int level);
• Instead of level value, we can use properties :
MIN_PRIORITY(1), NORM_PRIORITY(5) and
MAX_PRIPORITY(10).
• To obtain the current priority, use getPriority()
method.
– Syntax: int getPriority();
• Example program

You might also like