0% found this document useful (0 votes)
18 views

Oopjava Unit IV.pptx

The document provides an overview of multithreading in Java, explaining the concepts of multitasking, process-based multitasking, and thread-based multitasking. It details the lifecycle of a thread, methods for creating and managing threads, thread priorities, synchronization, and inter-thread communication. Additionally, it covers annotations in Java, including built-in and custom annotations, their categories, and usage.

Uploaded by

andemhindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Oopjava Unit IV.pptx

The document provides an overview of multithreading in Java, explaining the concepts of multitasking, process-based multitasking, and thread-based multitasking. It details the lifecycle of a thread, methods for creating and managing threads, thread priorities, synchronization, and inter-thread communication. Additionally, it covers annotations in Java, including built-in and custom annotations, their categories, and usage.

Uploaded by

andemhindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Object Oriented Programming Through JAVA

By
Dr. Srikanth Lakumarapu
Associate Professor
Dept. Of CSE
CVR COLLEE OF ENGINEERING

Unit-IV Multithreading and Modules


Multitasking
• Multitasking is a process of executing multiple tasks simultaneously.
• We use multitasking to maximum utilize the CPU.

• This Multitasking can be achiveed in two ways:


1. Process-Based Multitasking (Multiprocessing)
2. Thread-Based Multitasking (Multithreading)
Multiprocessing
1. Process-Based Multitasking:
Achieving multitasking through executing multiple processes simultaneously is
called as process based multitasking or multiprocessing.
• Each process has an address in memory. In other words, each process allocates
a separate memory area.
• A process is heavyweight.
• Cost of communication between the process is high.
• Switching from one process to another requires some time for saving and
loading registers, memory maps, updating lists, etc.
Multithreading
2) Thread-based Multitasking (Multithreading):
Achieving multi tasking through executing multiple threads simultaneously is
called as Thread based multitasking or Multithreading.
• Threads share the same address space.
• A thread is lightweight.
• Cost of communication between the thread is low.
Advantages of Multithreading
• 1) It doesn't block the user because threads are independent and you can
perform multiple operations at the same time.
• 2) You can perform many operations together, so it saves time.
• 3) Threads are independent, so it doesn't affect other threads if an exception
occurs in a single thread.

• What is Thread :
• A thread is a lightweight subprocess, the smallest unit of processing. It is a
separate path of execution.
Multiple Processes and Threads
Life cycle of Thread
• In Java, a thread always exists in any one of the following states.
• These states are:
1. New
2. Active
3. Blocked / Waiting
4. Timed Waiting
5. Terminated
Life cycle of Thread
Creation of a Thread
• There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Thread class
• Thread class is part of the java.lang package.
• It is used to create and manage threads in a program.
Key Constructors:
1. Thread(): Creates a new thread without specifying any target.
• When this thread's start() method is called, it executes the run() method of the
thread itself (i.e., the subclass's run() method if it's been overridden)
• Ex: Thread t = new Thread();
2. Thread(String name): Creates a new thread with a specified name, but
without any Runnable target.
• The thread will run its own run() method.
• Ex: Thread t = new Thread("MyThread");
Thread class Constructors
3. Thread(Runnable target): Creates a new thread that will run the run()
method of the given Runnable target object.
• Ex: Runnable r = new MyRunnable();
• Thread t = new Thread(r);
4. Thread(Runnable target, String name):Creates a new thread that will run
the run() method of the given Runnable target, and assigns a name to the thread.
• Ex: Thread t = new Thread(new MyRunnable(), "MyThread");
5. Thread(ThreadGroup group, String name): Creates a new thread within a
specified thread group, assigning the thread a name.
• Ex: ThreadGroup group = new ThreadGroup("MyGroup");
• Thread t = new Thread(group, "MyThread");
Thread class Constructors
6. Thread(ThreadGroup group, Runnable target): Creates a new thread
within a specified thread group. The thread will run the run() method of the given
Runnable target.
Ex: ThreadGroup group = new ThreadGroup("MyGroup");
Thread t = new Thread(group, new MyRunnable());
7. Thread(ThreadGroup group, Runnable target, String name): Creates a
new thread within a specified thread group, assigning the thread a name, and runs
the run() method of the provided Runnable target.
Ex: ThreadGroup group = new ThreadGroup("MyGroup");
Thread t = new Thread(group, new MyRunnable(), "MyThread");
Thread class methods
1. void start(): Starts the thread by making it runnable, which internally calls the
run() method.
2. void run(): Defines the task the thread will execute. You usually override this
method in a subclass or pass a Runnable to the Thread constructor.
3. static void sleep(long millis): Pauses the execution of the current thread for a
specified number of milliseconds.
4. void join(): Causes the current thread to wait until this thread finishes its execution.
5. void join(long millis): Causes the current thread to wait for this thread to finish or
for a specified number of milliseconds.
6. void interrupt(): Interrupts a thread that is currently sleeping, waiting, or otherwise
paused, causing it to throw an InterruptedException.
7. boolean isInterrupted(): Checks whether the thread has been interrupted, without
clearing the interrupt status.
Thread class methods
8. void setDaemon(boolean on): Marks a thread as a daemon thread, which
means it runs in the background and terminates when all user threads finish.
9. boolean isDaemon(): Checks if the thread is a daemon thread.
10. void setName(String name): Changes the name of the thread.
11. String getName(): Retrieves the name of the thread.
12. void yield(): Temporarily pauses the currently executing thread, giving other
threads a chance to execute.
13. void setPriority(int newPriority): Sets the priority of the thread (from 1
to 10, with higher values being more important).
14. int getPriority(): Returns the priority of the thread.
Thread class methods
15. Thread.State getState(): Retrieves the current state of the thread (e.g., NEW,
RUNNABLE, BLOCKED, WAITING, TERMINATED).
16. boolean isAlive(): Checks if the thread is still running (i.e., it has been started and
not yet terminated).
Thread Information and Utility Methods:
17. static Thread currentThread(): Returns a reference to the currently executing
thread.

18. long getId(): Returns the unique ID of the thread.

19. static int activeCount(): Returns the number of active threads in the current
thread’s thread group.
Runnable interface
• The Runnable interface is a part of java.lang package.
• It is one of the key interfaces for implementing multithreading.
• When a class implements the Runnable interface, it provides a way to define a
task that can be run by a thread without extending the Thread class.
• This is beneficial because Java doesn’t support multiple inheritance, so by
implementing Runnable, a class can still extend another class.
• The Runnable interface is a functional interface with just one method:
public interface Runnable { public abstract void run(); }
Creating multiple Threads
• Multiple threads can be created by
• creating multiple threads of different thread classes
• Or

• creating multiple threads of same thread class.


Thread Priorities
• Thread priorities are used to influence the order in which threads are scheduled
for execution by the thread scheduler.
• While thread priority does not guarantee the exact order of execution, it
provides a hint to the operating system about the relative importance of a
thread.
• Threads with higher priorities are generally executed in preference to threads
with lower priorities, but scheduling is ultimately managed by the JVM and the
operating system.
Thread Priority in Java:
• Default Priority: Every thread is assigned a default priority when it is created.
• The default priority of a thread is inherited from the thread that created it.
Thread Priorities
• Priority Range: The valid range of thread priorities in Java is from integer 1 to
10.
• It also defined by the constants in the Thread class:
• Thread.MIN_PRIORITY (1) – the minimum priority
• Thread.NORM_PRIORITY (5) – the normal priority (default)
• Thread.MAX_PRIORITY (10) – the maximum priority

Setting and Getting Priority:


• void setPriority(int priority): Sets the priority of the thread to the specified
value.
• int getPriority(): Returns the current priority of the thread.
Thread Synchronization
Thread Synchronization is a concept which ensure that only one thread accesses
a shared resource at a time.
It helps prevent issues such as race conditions.
This is typically achieved using synchronized methods or blocks.
1. Synchronized Methods: A simple way to synchronize access to a method.
When a thread invokes a synchronized method, it acquires the lock for that
object, blocking other threads from entering any synchronized methods on
the same object.
2. Synchronized Blocks: Synchronized blocks provide more fine-grained
control over synchronization. You can synchronize only a portion of a
method, allowing other threads to access non-synchronized parts.
Thread Synchronization
3. Using ReentrantLock: The ReentrantLock class in java.util.concurrent.locks
provides more flexibility compared to the synchronized keyword.
It allows for features like try-locking and timed locking.
4. Using ReadWriteLock: When you have multiple threads reading and fewer
threads writing, ReadWriteLock can improve performance by allowing multiple
readers to access the resource simultaneously.
Inter Thread Communication
Inter-thread communication in Java is a mechanism that allows threads to
communicate with each other, enabling them to coordinate their actions and
share information.
This is essential in multi-threaded applications, particularly when threads depend
on the state of shared resources.
The primary methods used for inter-thread communication in Java are wait(),
notify(), and notifyAll().
These methods are part of the Object class.
Wait and Notify: Threads can wait for a condition to be true (using wait()) and
can be notified when that condition changes (using notify() or notifyAll()).
Example: Producer-Consumer Problem
Annotations
• Annotations are special markers in Java code that begin with the @ symbol.
• They do not directly affect the program's semantics but can be used to provide
information to the compiler or to runtime frameworks.
• Annotations help to associate metadata (information) to the program elements
i.e. instance variables, constructors, methods, classes, etc.
• Annotations basically are used to provide additional information, so could be
an alternative to XML and Java marker interfaces.
Hierarchy of Annotations in Java

Annotations

Documentation Programming
Annotations Annotation

Buitlnin Annotation Custom Annotaion


Documentations Annotations
• Documentation annotations are primarily focused on creating documentation
for your code using Javadoc.
• It is a tool that generates HTML documentation from comments and
annotations in your code.
• Javadoc includes specific annotations (also known as tags) that are used to
describe classes, methods, fields, parameters, exceptions, and more.
• Commonly Used Javadoc Annotations:
@author, @version, @param, @return, @see, @since, @deprecated,
@exception, {@link ...}, {@code ...}, ….etc
Built-in Annotations
• @Override
• @SuppressWarnings
• @Deprecated
Meta-Annotations
• Built-In Java Annotations used in other annotations
@Target - Specifies the kinds of program elements to which an annotation
type is applicable (e.g., methods, classes).
@Retention - Specifies how long annotations with the annotated type are to be
retained.
@Documented - Marks the annotation for inclusion in the Javadoc.
@Inherited - Indicates that an annotation type is automatically inherited.
Meta-Annotations
• Target Types
• Annotations can be applied to various program elements such as:
• ElementType.TYPE - for classes, interfaces, or enums.
• ElementType.FIELD - for fields.
• ElementType.METHOD - for methods.
• ElementType.PARAMETER - for parameters.
• ElementType.CONSTRUCTOR - for constructors.
Meta-Annotations
• Retention Policies
• When creating an annotation, you must specify its retention policy using the
@Retention annotation. The options are:
• RetentionPolicy.SOURCE - Annotations are only available in the source
code and discarded during compilation.
• RetentionPolicy.CLASS - Annotations are recorded in the class file but are
not available at runtime.
• RetentionPolicy.RUNTIME - Annotations are recorded in the class file and
available at runtime using reflection.
Categories of Annotations
• There are broadly 3 categories of annotations as listed:
1. Marker Annotations - @Override
2. Single value Annotations -@SupressWarning(“uncheked”)
3. Multi Value Annotations -@Entity(table = "users", schema = "public")
User-defined (Custom) Annotations
User-defined annotations can be used to annotate program elements, i.e.
variables, constructors, methods, etc. These annotations can be applied just
before the declaration of an element (constructor, method, classes, etc).
Syntax: Declaration
[Access Specifier] @interface<AnnotationName>
{
DataType <Method Name>() [default value];
}
User-defined (Custom) Annotations
certain points to be noted before implementing user-defined annotations.
• AnnotationName is an interface.
• The parameter should not be associated with method declarations and throws
clause should not be used with method declaration.
• Parameters will not have a null value but can have a default value.
• default value is optional.
• The return type of method should be either primitive, enum, string, class name,
or array of primitive, enum, string, or class name type.

You might also like