0% found this document useful (0 votes)
2 views26 pages

Unit 2

Unit 2 covers essential Java concepts including Exception Handling, Input/Output Basics, and Multithreading. It explains the importance of exception handling in managing runtime errors, the differences between built-in and user-defined exceptions, and the structure of try-catch-finally blocks. Additionally, it introduces Java I/O operations and the use of streams for file management.

Uploaded by

Ak
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)
2 views26 pages

Unit 2

Unit 2 covers essential Java concepts including Exception Handling, Input/Output Basics, and Multithreading. It explains the importance of exception handling in managing runtime errors, the differences between built-in and user-defined exceptions, and the structure of try-catch-finally blocks. Additionally, it introduces Java I/O operations and the use of streams for file management.

Uploaded by

Ak
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/ 26

Unit 2

2.1 Exception Handling: The Idea behind Exception, Exceptions & Errors, Types of
Exception, Control Flow in Exceptions, JVM Reaction to Exceptions, Use of try, catch,
finally, throw, throws in Exception Handling, In-built and User Defined Exceptions, Checked
and Un-Checked Exceptions.
2.2 Input /Output Basics: Byte Streams and Character Streams, Reading and Writing File in
Java.
2.3 Multithreading: Thread, Thread Life Cycle, Creating Threads, Thread Priorities,
Synchronizing Threads, Inter-thread Communication.

2.1 Exception Handling: One of the most effective mechanisms in Java for managing
runtime faults and preserving the regular operation of the application is exception handling.
An exception in Java is an occurrence that prevents the program from operating normally. It
is a runtime object that is thrown.
An approach to deal with runtime problems like ClassNotFoundException, IOException,
SQLException, RemoteException, etc. is called exception handling.
Benefits of Exceptional Management:
Preserving the application's regular flow is the fundamental benefit of exception handling.
Because an exception typically causes the application's usual flow to be disrupted, exception
handling is necessary. Let us examine an example:

In a Java program with ten statements, let's say an exception happens at statement five. In this
case, statements six through ten will not be performed. However, the remaining sentences
will be carried out when we handle exceptions. In Java, exception handling is used for this
reason.
2.1.1 The Idea behind Exception: An unwanted or unexpected occurrence that arises during
program execution, or during run time, which interferes with the program's instructions' usual
flow is referred to as an exception in Java. The application has the capability to detect and
manage exceptions. A method creates an object when an exception happens within it. We
refer to this object as the exception object. It includes details about the exception, including
its name, description, and the program's status at the time it happened.
Major reasons why an exception Occurs:

Dr. Ratnesh Kumar Shukla Page 1


 Invalid user input
 Device failure
 Loss of network connection
 Physical limitations (out-of-disk memory)
 Code errors
 Opening an unavailable file
2.1.2 Exceptions & Errors: The Java virtual machine (JVM) running out of memory,
memory leaks, stack overflow issues, library incompatibilities, infinite recursion, and other
unrecoverable circumstances are examples of errors. We shouldn't attempt to address
mistakes because they are typically outside of the programmer's control.
Difference between Error and Exception:
Let's talk about the most crucial component, which is the distinction between an exception
and an error, which is as follows:
Error: An error is a serious issue that should not be attempted to be resolved by a reasonable
application.
Exception: An exception describes a set of circumstances that a sensible program would
attempt to detect.
The foundation class of the hierarchy is called Throwable, and all exception and error kinds
are subclasses of this class. Exception is in charge of one branch. The extraordinary
circumstances that user applications should detect are handled by this class. One instance of
this type of exception is NullPointerException. The Java run-time system (JVM) uses a
different branch called Error to represent faults pertaining to the run-time environment itself
(JRE). One instance of this kind of problem is StackOverflowError.

Dr. Ratnesh Kumar Shukla Page 2


2.1.3 Types of Exception: Java has a number of exception kinds that are related to its
different class libraries. Users of Java can also create custom exceptions.

Exceptions can be categorized in two ways:


1. Built-in Exceptions
 Checked Exception
 Unchecked Exception
2. User-Defined Exceptions
2.1.3.1 Built-in Exceptions: The exceptions that come with Java libraries are known as built-
in exceptions. These exclusions make sense in explaining specific error scenarios. The list of
significant Java built-in exceptions is provided below.
2.1.3.1.1 Checked Exceptions: Because the compiler verifies verified exceptions during
compile time, they are also known as compile-time exceptions. Whether or not the
programmer handles the exception is ensured by the compiler. If the exception is not handled
by the programmer, the system will display a compilation error.
ArithmeticException: When an extraordinary circumstance arises during an arithmetic
operation, the exception known as ArithmeticException is raised.

ArrayIndexoutofBoundsException: This exception is raised when an illegal index is used


to access an array. The index might be negative, larger than, or equal to the array's size.
ClassNotFoundException: When we attempt to access a class whose definition is not
available, we receive the ClassNotFoundException.
NullPointerException: When referring to the components of a null object, this exception is
raised. Null stands for nothing.
IllegalStateException: When a method is not accessed for a specific application operation,
this exception will raise an error or error message. Under the unchecked exception, it fits in.

Dr. Ratnesh Kumar Shukla Page 3


2.1.3.1.2 Unchecked Exceptions: The checked exceptions and the unchecked exceptions are
exactly the opposite. These exceptions won't be checked by the compiler during compilation.
Put simply, a program would not produce a compilation error if it threw an unchecked
exception, even if we didn't handle or declare it. Typically, it happens when a user enters
incorrect data into the application.
FileNotFoundException: When a file cannot be opened or accessed, the File Not Found
Exception is raised.
IOException: When an input-output operation fails or is interrupted, an IOException is
thrown.
InterruptedException: When a thread is interrupted while it is processing, sleeping, or
waiting, an InterruptedException exception is raised.
NoSuchFieldException: This exception is raised when a class lacks the designated field or
variable. When trying to access a method that is not found, the exception
NoSuchMethodException is raised.
NumberFormatException: When a method fails to convert a string into a numeric format,
this exception is raised.
RuntimeException: This stands for an exception that happens when the program is running.
StringIndexOutOfBoundsException: String class methods throw the
StringIndexOutOfBoundsException exception to signal that an index is either negative or
larger than the string's length.
IllegalArgumentException: When the method receives an argument that does not precisely
fit to the specified relation or condition, this exception will issue an error or error statement.
Under the unchecked exception, it fits in.

2.1.3.2 User-Defined Exceptions: Occasionally, a condition may not be adequately


described by Java's built-in exceptions. "User-defined Exceptions" are exceptions that the
user can make in certain situations.
To create a user-defined exception, take the subsequent actions. As a subclass of the
Exception class, the user must construct an exception class. The user should create a subclass
of the Exception class since all exceptions are subclasses of it. This is carried out as:
Syntax:
class MyException extends Exception
He can write his own exception class using a default constructor.
Syntax:
MyException(){}

Dr. Ratnesh Kumar Shukla Page 4


Another option is to write a parameterized constructor that takes a string as an argument. This
can be used to record exception information. This allows us to transmit the string to the
superclass(Exception) constructor by calling it.
Syntax:
MyException(String str)
{
super(str);
}
An object to his exception class must be created, and it must be thrown using the throw
clause in order to raise an exception of a user-defined type, as follows:
Syntax:
MyException me = new MyException(“Exception details”);
throw me;
The program that follows shows you how to make your own MyException exception class.
Three arrays are used to collect account number, customer name, and balance information.
The details are shown using a for-loop in the main() method. Currently, if the balance in any
account is less than what is required to be in the account, an investigation is conducted.
If this is the case, "Balance amount is less" is displayed as a message and MyException is
raised.
Example:

Dr. Ratnesh Kumar Shukla Page 5


Output:

2.1.4 Control Flow in Exceptions: The exception is sent to the catch block, just like an
argument is passed into a method parameter, and the code in the (corresponding) catch block
is performed, if the type of exception that occurred is specified or handled in a catch block.
An issue (run time fault) that arose while a program was running is called an exception.
Compile-time exceptions, also referred to as checked exceptions, are those that elicit an
exception during the compilation process.
When an exception arises, the program stops suddenly at the line that raised it, stopping
execution of the remaining code. You must deal with exceptions if you want to avoid this.
Try, grab, and then block: Java includes a try-catch block method to manage exceptions.
The try block: Try blocks are positioned around code that could throw an exception.
Protected code is code that is enclosed in a try/catch block.
An exception simply propagates "up" to the caller if it arises outside of a try, catch and block.
There are several possible configurations for the control flow when an exception arises inside
a try, catch and block. The following scenarios are listed with indicators of the code blocks
that are part of the control flow in the given table:
Case try catch finally bottom
try throws no exception. y - y y
try throws a handled exception y y y y
try throws an unhandled exception y - y -
As demonstrated here, "bottom" in this context simply refers to any code that comes after the
finally block:
Example:
Test1.java

Dr. Ratnesh Kumar Shukla Page 6


Output:

Example:
Test.java

Output:

It's a prevalent misperception, particularly among C programmers switching to Java, that


regular control flow may be defined using exceptions. This is an abuse of the concept of
exceptions, which are limited to things that are defective or outside the program's direct
control.

Dr. Ratnesh Kumar Shukla Page 7


2.1.5 JVM Reaction to Exceptions: To handle the Exception object, the JVM must locate an
exception handler. It traverses the call stack backwards until it locates an exception handler
that corresponds to that specific class of Exception object (referred to as "catching" the
Exception in Java terminology).
JVM assumes responsibility for handling an exception if it is not expressly handled.
JVM will stop the program and stop all code execution once the exception has been handled.
Example:
JVM.java

Output:

2.1.6 Use of try: It is possible to provide a block of code that will be checked for mistakes
during execution by using the try statement. If an error happens in the try block, you may
specify a block of code that will be performed using the catch statement.
Try blocks in Java are used to contain code that has the potential to throw exceptions. The
method must be followed when using it.
The remainder of the block code won't run if an exception arises at that specific try block
statement. Therefore, it is advised against keeping code that won't throw an exception in the
try block.
Either the catch or finally blocks must come after the Java try block.
Syntax:
try
{
// statement(s) that might cause exception
}
2.1.7 Catch: The type of exception is declared within the argument and handled by the Java
catch block. Either the created exception type or the parent class exception (Exception) must

Dr. Ratnesh Kumar Shukla Page 8


be the defined exception. Declaring the created type of exception is, nevertheless, a wise
course of action.
The try block's uncertain situation is handled by the catch block. A catch block, which deals
with the exception raised in the related try block, always comes after a try block.
Syntax:
catch
{
// statement(s) that handle an exception
// examples, closing a connection, closing
// file, exiting the process after writing
// details to a log file.
}
Internal Working of Java try-catch block:

First, the JVM determines if the exception is handled or not. The JVM comes with a default
exception handler that takes care of the following functions in case the exception is not
handled:
 Prints out exception description.
 Prints the stack trace (Hierarchy of methods where the exception occurred).
 Causes the program to terminate.

Dr. Ratnesh Kumar Shukla Page 9


2.1.8 Finally: It is carried out following the catch block. When there are several catch blocks,
we use it to place some common code that will run whether or not an exception has
happened.
Below is an illustration of an exception that the system has generated:
Exception in thread "main"
java.lang.ArithmeticException: divide
by zero at ExceptionDemo.main(ExceptionDemo.java:5)
ExceptionDemo: The class name
main:The method name
ExceptionDemo.java:The file name
java:5:line number
2.1.9 Throw: Control can be moved from the try block to the catch block using the throw
keyword.

2.1.10 Throws in Exception Handling: For exception handling without a try and catch
block, utilize the throws keyword. It doesn't handle itself; instead, it describes the exceptions
that a method can throw to the caller.
A brief explanation of try, catch, finally. Throw and throws in below table:
Keyword Description
try This keyword is used to specify a block and this block must be followed by
either catch or finally. That is, we can‟t use try block alone.
catch This keyword must be preceded by a try block to handle the exception and
can be followed by a final block later.
finally This keyword is used to execute the program, whether an exception is
handled or not.
throw This keyword is used to throw an exception.
throws This keyword is used to declare exceptions.

2.1.11 In-built and User Defined Exceptions:


Built-in exceptions include ArithmeticException, ArrayIndexOutOfBoundExceptions,
ClassNotFoundExceptions, and so on. Occasionally, specific situations cannot be adequately
explained or described by the built-in exceptions. We must construct our own exceptions by
subclassing the Exception class and creating an exception class in order to describe these
scenarios. User-Defined Exceptions encompass the following kinds of exceptions.
Exceptions that are pre-existing and fall under the Java Exception class are known as built-in
exceptions. User-defined Exceptions are specified by the programmer or user.

Dr. Ratnesh Kumar Shukla Page 10


Difference between User-Defined and Built-In Functions:
User-Defined Functions Built-In Functions
Created by developers to perform custom Predefined functions provided by the PHP
tasks. language.
Defined using the function keyword followed Available for use without the need for
by a name and code block. explicit definition.
Can accept parameters and return values. Cover a wide range of functionalities
including string manipulation, array
operations, file handling, etc.

2.1.12 Checked and Un-Checked Exceptions:


Checked Exceptions Unchecked Exceptions
Occur at compile time. Occur at runtime.
The compiler checks for a checked exception. The compiler doesn‟t check for exceptions.
Can be handled at the compilation time. Can‟t be caught or handled during
compilation time.
The JVM requires that the exception be The JVM doesn‟t require the exception to be
caught and handled. caught and handled.
Example of Checked exception- „File Not Example of Unchecked Exceptions- „No
Found Exception‟ Such Element Exception‟
At compile time, these exceptions are The checked exceptions and these exceptions
examined. Compile time handling is also are exactly the reverse. At compile time,
applied to these exceptions. these exceptions are not handled and
examined.
When a method throws a checked exception, The exceptions avoid the compiler's
the code reports a compilation problem. The attention, therefore the code assembles
exception cannot be handled by the compiler without any errors. These exceptions are the
by itself. outcome of programming logic mistakes
made by the user.
These exceptions typically arise when there The majority of these exceptions are the
is an excessive likelihood of failure. result of programming errors.
To handle the checked exception, the try- When there is an unchecked exception, it is
catch and try-finally blocks must be not required.
provided.
Throws is the keyword used to propagate They spread on their own naturally.
these exceptions.
IOException, DataAccessException, ArithmeticException, InvalidClassException,
InterruptedException, and other common NullPointerException, and other frequently
errors are frequently examined. occurring unchecked exceptions are
examples.

Dr. Ratnesh Kumar Shukla Page 11


2.2 Input /Output Basics: To process the input and generate the output, Java I/O (Input and
Output) is utilized. Java speeds up I/O operations by utilizing the stream idea. All of the
classes needed for input and output operations are included in the java.io package. With Java
I/O API, file management is possible.
The java.io package contains the Java IO API. Almost all of the classes a user needs to
execute input and output (I/O) in Java are included in this package. The input and output to
files, network streams, etc., is the main focus of the Java IO package. Sadly, classes for
opening network sockets, which are necessary for network communication are absent from
the Java IO package.
Typically, using the java.io package entails reading some basic data from a source and
writing it to a destination. The idea of a program receiving input data from a source and
creating output based on it to a destination is perfectly shown in the graphic below.

Java IO Streams: Within the programming community, a stream is defined as an array of


data. Because of its resemblance to an endless stream of water, it is called a stream. Data
flows that a user can read from or write to are known as Java IO streams. A stream lacks the
idea of indexing read or write data, just like an array does. Either a data source or a data
destination is connected to a stream.

Dr. Ratnesh Kumar Shukla Page 12


Java Streams reads and writes data using two main streams. These are the following:
1. Input Stream
2. Output Stream.
Input Stream: In a Java application, data is read from a source via an input stream. Anything
can be considered data, including files, arrays, peripheral devices, and sockets. The java.io
class in Java. The fundamental class for all Java IO input streams is called Input stream.

Java IO InputStream Methods:


1. read(): The following byte of data is read from the Input Stream using the read() method. A
scale from 0 to 255 is used to pass the value byte. The value -1 is passed if there isn't a single
free byte since the stream's end has reached.
mark(int arg): The input stream's current position is marked using the mark(int arg) method.
It establishes the read to limit, or the most bytes that can be read prior to the mark location
becoming invalid.

Dr. Ratnesh Kumar Shukla Page 13


reset(): The mark() function calls the reset() method. The input stream's position is adjusted
back to the designated location.
close(): The close() function closes the input stream and gives Garbage Collector access to
the system resources connected to it.
read(byte [] arg): The number of bytes of arg.length from the input stream to the buffer array
arg is read using the read(byte [] arg) method. An int is returned along with the bytes that the
read() function read. In the event that the length is zero, no bytes are read and, barring an
attempt to read at least one byte, 0 is returned.
skip(long arg): The input stream's arg bytes can be skipped and discarded using the skip(long
arg) method.
markSupported(): This function determines whether the input stream is capable of supporting
the mark and reset procedures. By default, the Java IO InputStream's markSupported function
returns false.
OutputStream: Write data (a file, an array, a peripheral device, or a socket) to a destination
via an output stream. The java.io belongs in java class. The fundamental class for all Java IO
output streams is called OutputStream.
Java IO OutputStream Methods:
flush(): The outputStream is flushed using the flush() method. Writing out the buffered
output bytes is required by this approach.
close(): The close() method is used to end the outputStream and free up any associated
system resources.
write(int b): The given byte can be written to the outputStream using the write(int b)
method.
write(byte [] b): To write bytes of length b.length from the designated byte array to the
outputStream, utilize the write(byte [] b) method.
2.2.1 Byte Streams and Character Streams: Java streams come in two varieties: byte
streams and character streams. Eight-bit byte input and output are handled via byte streams.
Bit streams can be used to read and write binary data. Input and output operations for 16-bit
Unicode are carried out via the character stream.
There may be differences between these streams in terms of the kind and manner of data
handling.
2.2.1.1 Character Stream: Unicode conventions are used in Java character storage. Data can
be read and written automatically character by character using the character stream. Character
streams, for instance, are used to read from the source and write to the destination using
FileReader and FileWriter.

Dr. Ratnesh Kumar Shukla Page 14


Example:

2.2.1.2 Byte Stream: Data is processed byte by byte (8 bits) by byte streams. For instance,
FileOutputStream is used to write to the destination and FileInputStream is used to read from
the source.

Dr. Ratnesh Kumar Shukla Page 15


2.2.1.3 Difference between Character Stream and Byte Stream: Because they enable data
to be read from or written to a source or destination, streams in Java are used for input and
output operations.
Aspect Character Stream Byte Stream
Data Handling Handle character-based data Handle raw binary data
Representation Classes end with "Reader" or Classes end with "InputStream"
"Writer" or "OutputStream"
Suitable for Textual data, strings, human- Non-textual data, binary files,
readable info multimedia
Character Encoding Automatic encoding and No encoding or decoding
decoding
Text vs non-Text data Text-based data, strings Binary data, images, audio, video
Performance Additional conversion may Efficient for handling large
impact performance binary data
Handle Large Text May impact performance due to Efficient, no encoding overhead
Files encoding
String Operations Convenient methods for string Not specifically designed for
operations string operations
Convenience Methods Higher-level abstractions for text Low-level interface for byte data
data
Reading Line by Line Convenient methods for reading Byte-oriented, no built-in line-
lines reading methods
File Handling Read/write text files Read/write binary files
Network Sending/receiving text data Sending/receiving binary data
Communication
Handling Not designed for handling binary Suitable for handling binary
Images/Audio/Video data directly multimedia data

Dr. Ratnesh Kumar Shukla Page 16


Text Encoding Supports various character No specific text encoding support
encodings

2.2.2 Reading and Writing File in Java: Java provides the java.nio.file API to read and
write files. The InputStream class is the superclass of all classes representing an input stream
of bytes.
2.2.2.1 Reading files in Java: The Files.readAllBytes method can be used to read a text file.
The following listing illustrates how to use this approach.

To read a text file line by line into a List of type String structure you can use the
Files.readAllLines method.

Files.readAllLines uses UTF-8 character encoding. It also ensures that file is closed after all
bytes are read or in case an exception occurred.
2.2.2.1.1 Reading file line by line in Java: By providing a stream, the Files.lines method
enables reading a file line by line. It is possible to map and filter this stream. Try-with-
resource statements should enclose Files.lines since they don't close the file after its contents
are read.
The following example filters out empty lines and removes extra whitespace at the end of
each line.

2.2.2.2 Write file in Java: We'll look at many approaches of writing in a file using the Java
programming language. Because it is used in file handling in Java, the Java FileWriter class
is character-oriented and is used to write character-oriented data to files. In Java, there are
numerous classes and methods that can accomplish the following goals, hence there are
numerous ways to write into a file:

Dr. Ratnesh Kumar Shukla Page 17


1. Using writeString() method.
2. Using FileWriter Class.
3. Using BufferedWriter Class.
4. Using FileOutputStream Class.

2.2.2.2.1 Using writeString() method: Java version 11 supports this approach. Four
parameters can be passed to this method. The file location, charset, character sequence, and
options are these. In order for this method to write into a file, the first two parameters must be
provided. The characters are written as the file's content. In addition to returning the file path,
it has four error types. It works well when the file's content is brief.

Example: It demonstrates how to put data into a file using the writeString() function under
the Files class. The filename is associated with a path where the content is to be written using
a different class called Path. The readString() function of the Files class allows you to read
the contents of any file that is currently in existence and verify that the content is written
correctly in the file.

2.2.2.2.2 Using FileWriter Class: Writing to the file with the FileWriter class is an even
better choice if the content of the file is brief. The writeString() method is also used to write
the character stream as the file's content. Both the default character encoding and buffer size
in bytes are defined by the constructor of this class.

The usage of the FileWriter class to write material to a file is demonstrated in the example
below. To write into a file, an instance of the FileWriter class object must be created using
the filename. The value of the text variable is then written to the file using the write()
method. An IOException will be raised and the error message will be written from the catch
block if there is a problem when writing the file.

2.2.2.2.3 Using BufferedWriter Class: Text can be written to a character-output stream


using it. Although a huge buffer size can be given, it has a default buffer size. Writing
characters, strings, and arrays can benefit from its use. If prompt output is not needed, it is
preferable to wrap this class with any writer class for writing data to a file.

2.2.2.2.4 Using FileOutputStream Class: The raw stream data is written to a file using it.
Only text can be written to a file using the FileWriter and BufferedWriter classes; binary data
can be written using the FileOutputStream class.

The following example illustrates how to use the FileOutputStream class to write data into a
file. To put data into a file, the class object must also be created with the filename. Here, the
write() method is used to transform the string content into a byte array that is written to the
file.

Dr. Ratnesh Kumar Shukla Page 18


2.3 Multithreading: In Java, multithreading refers to the ability to run many threads at once.
Designed to be lightweight, threads allow for parallelism and the execution of multiple
processes at once, improving the speed and responsiveness of applications. You can construct
threads in Java by either implementing the Runnable interface or by extending the Thread
class. In order to fully utilize the capabilities of contemporary processors with many cores
and to enable effective system resource usage, multithreading is essential. To prevent
conflicts and inconsistent data, it is crucial to manage threads and guarantee appropriate
synchronization. This article will discuss the fundamentals of multithreading, its advantages,
and several methods for establishing and controlling threads in Java.

The smallest processing unit is a thread, which is a light subprocess. Multitasking is


accomplished through the employment of both multiprocessing and multithreading.

Process and Programs: All we have on our computers are programs (or a group of programs
called software) that we use as apps. These are the programs that run when we start an
application, and we use the Operating System, another program, to interface with these
programs.
So, if we give it some serious thought, there are only two possible states for these programs:
under execution and not under execution. All that means is that every program on your
computer is either in use or not. Thus, processes are programs that are in use or being carried
out.
Therefore, the following definition of a program and a process can be understood as follows:
"A process is a program under execution."

2.3.1 Thread: The smallest unit of a process is called a thread. Indeed, it is crucial to realize
that a thread is the smallest unit of a process, not a program, as a process is what executes
while a program does not. Here's an example to assist us comprehend this. Please take note
that this is not a precise explanation of how threads operate; rather, it is merely an example to
show what they are.

Each thread is autonomous. Other threads are unaffected if an exception occurs in one of
them. It makes use of shared memory.

Dr. Ratnesh Kumar Shukla Page 19


A thread is run inside the process, as seen in the above figure. The threads can switch
between different contexts. Within the operating system, a single process may contain several
threads.

2.3.1.1 Java Thread class: To accomplish thread programming, Java offers the Thread class.
Constructors and methods to generate and operate on a thread are provided by the Thread
class. The Thread class implements the Runnable interface and extends the Object class.

Java Thread Methods: The table below includes representations of various significant Java
methods.

S.No. Modifiers and Methods Descriptions


Types
1 void start() It is used to start the execution of the thread.
2 void run() It is used to do an action for a thread.
3 static void sleep() It sleeps a thread for the specified amount of
time.
4 static Thread currentThre It returns a reference to the currently executing
ad() thread object.
5 void join() It waits for a thread to die.
6 int getPriority() It returns the priority of the thread.
7 void setPriority() It changes the priority of the thread.
8 string getName() It returns the name of the thread.
9 void setName() It changes the name of the thread.
10 long getId() It returns the id of the thread.
11 boolean isAlive() It tests if the thread is alive.
12 static void yield() It causes the currently executing thread object to
pause and allow other threads to execute
temporarily.
13 void suspend() It is used to suspend the thread.
14 static int enumerate() It is used to copy every active thread's thread
group and its subgroup into the specified array.
15 string toString() It is used to return a string representation of this
thread, including the thread's name, priority, and
thread group.

2.3.2 Thread Life Cycle: A thread in Java is always present in any of the following states.
These states consist of:

1. New: a recently formed thread that is not yet in the execution phase.

2. Active: It is awaiting the distribution of resources and is either in operation or prepared for
execution.

3. Blocked / Waiting: Anticipating the acquisition of a monitor lock in order to enter or re-
enter a synchronized block or method and waiting for an untimed operation to be completed
by another thread.

Dr. Ratnesh Kumar Shukla Page 20


4. Timed Waiting: Waiting a predetermined amount of time for another thread to complete a
particular task.

5. Terminated: It has finished being carried out.

The figure below depicts the various states that a thread goes through over its lifetime.

2.3.2.1 New: A new thread is always created in the newly created state. The code has not yet
been executed for a thread in the new state since it has not been run.

2.3.2.2 Active: A thread transitions from the new state to the active state when it calls the
start() function. There are two states that make up the active state: running and runnable.

2.3.2.2.1 Runnable: A thread that is ready to run is subsequently advanced to the runnable
state. In the runnable state, the thread can be running or ready to run at any time. It is the
thread scheduler's responsibility to provide thread time to run, i.e. move the thread into the
running state. When a thread calls start(), it transitions from the new state to the active state.
The active state has two states: runnable and running.

A program that a support multithreading allocates a fixed amount of time to each individual
thread. Each thread runs for a short period of time, and when that time slice is up, the thread
voluntarily hands over the CPU to the other thread, allowing the other threads to run for their
own slice of time. Whenever such a circumstance arises, all threads that are ready to run and
waiting for their turn to run are in the runnable state. In the runnable state, threads are stored
in a queue.

2.3.2.2.2 Running: When a thread receives the CPU, it transitions from the runnable to the
running state. The most common transition in a thread's state is from runnable to running,
then back to runnable.

2.3.2.3 Blocked/Waiting: When a thread is inactive for an extended period of time (but not
permanently), it is classified as either blocked or waiting.

Dr. Ratnesh Kumar Shukla Page 21


For example, a thread (let's call it A) may want to print data from the printer. However, the
second thread (let's call it B) is using the printer to print some data. Thus, thread A must wait
for thread B to utilize the printer. Thus, thread A is in the blocked condition. A thread in the
blocked state is unable to execute any instructions and hence does not consume any CPU
cycles. As a result, we can say that thread A is inactive until the thread scheduler reactivates
it, which is currently in the waiting or blocked state.

2.3.2.4 Time Waiting: Sometimes waiting causes famine. For example, a thread (named A)
has entered a critical portion of code and refuses to leave it. In such a case, another thread
(named B) must wait indefinitely, resulting in starvation. To avoid this issue, thread B is set
to a timed waiting state. Thus, the thread is in a waiting state for a set amount of time, not
eternally. The sleep() method on a specified thread is a real-world example of timed waiting.
The sleep() function puts the thread into a timed wait state. When the timer runs out, the
thread wakes up and resumes its execution from where it left off before.

2.3.2.5 Terminated: A thread enters the termination state for the following reasons:

 When a thread completes its task, it exists or ends naturally.


 Abnormal termination happens when there are uncommon events, such as an
unhandled exception or a segmentation fault.

A terminated thread signifies that it is no longer in the system. In other words, the thread is
no longer active, and it cannot be respawned.

2.3.3 Creating Threads: There are two methods for creating a thread:

1. By extending the Thread class.

2. By implementing the Runnable interface.

2.3.3.1 Thread class: Thread classes provide constructors and methods for creating and
operating with threads. Thread extends the Object class and implements the Runnable
interface.

2.3.3.1.1 Commonly used Constructors of Thread class:

 Thread()
 Thread(String name)
 Thread(Runnable r)
 Thread(Runnable r,String name)

2.3.3.1.2 Commonly used methods of Thread class:

1. public void run(): It is used to perform action for a thread.


2. public void start(): It starts the execution of the thread. JVM calls the run() method
on the thread.
3. public void sleep(long miliseconds): It causes the currently executing thread to sleep
(temporarily cease execution) for the specified number of milliseconds.

Dr. Ratnesh Kumar Shukla Page 22


4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): It waits for a thread to die for the specified
miliseconds.
6. public Thread currentThread(): It returns the reference of currently executing
thread.
7. public int getId(): It returns the id of the thread.
8. public Thread.State getState(): It returns the state of the thread.
9. public boolean isAlive(): It tests if the thread is alive.
10. public void yield(): It causes the currently executing thread object to temporarily
pause and allow other threads to execute.
11. public void suspend(): It is used to suspend the thread(depricated).
12. public void resume(): It is used to resume the suspended thread(depricated).
13. public void stop(): It is used to stop the thread(depricated).
14. public int getPriority(): It returns the priority of the thread.
15. public int setPriority(int priority): It changes the priority of the thread.
16. public String getName(): It returns the name of the thread.
17. public void setName(String name): It changes the name of the thread.
18. public boolean isDaemon(): It tests if the thread is a daemon thread.
19. public void setDaemon(boolean b): It marks the thread as daemon or user thread.
20. public void interrupt(): It interrupts the thread.
21. public boolean isInterrupted(): It tests if the thread has been interrupted.
22. public static boolean interrupted(): It tests if the current thread has been
interrupted.

2.3.3.2 Runnable interface: Any class that expects its instances to be executed by a thread
should implement the Runnable interface. The Runnable interface has only one function,
named run().

 public void run(): is used to perform action for a thread.

The start() function of the Thread class is used to initiate a freshly formed thread. It carries
out the following tasks:

 A new thread starts (with new call stack).


 The thread moves from new state to the Runnable state.
 When the thread gets a chance to execute, its target run() method will run.

2.3.4 Thread Priorities: Every thread has a priority. Priorities are represented by a number
from 1 to 10. In most circumstances, the thread scheduler assigns threads based on their
priority (known as pre-emptive scheduling). However, it is not assured because it is
dependent on the JVM specification as to which scheduling is chosen. It is worth noting that,
in addition to the JVM, a Java programmer can directly assign thread priority in a Java
program.

Priorities in threads is a concept in which each thread has a priority, which can be translated
into layman's terms as "every object" and is represented by numbers ranging from 1 to 10.

Dr. Ratnesh Kumar Shukla Page 23


 The default priority is set to 5 as excepted.
 Minimum priority is set to 1.
 Maximum priority is set to 10.

Here 3 constants are defined in it namely as follows:

1. public static int NORM_PRIORITY

2. public static int MIN_PRIORITY

3. public static int MAX_PRIORITY

Let us look at how to get and set the priority of a thread in Java.

public final int getPriority(): The java.lang.Thread.getPriority() method returns the priority
of the given thread.

public final void setPriority(int newPriority): The Java.lang.Thread.setPriority() modifies


or assigns the thread's priority to newPriority. If the newPriority value exceeds the range of 1
(minimum) to 10 (maximum), the method throws an IllegalArgumentException.

2.3.5 Synchronizing Threads: Synchronization in Java refers to the ability to govern


different threads' access to a shared resource.

Java Synchronization is a superior solution when only one thread needs to access a shared
resource.

Java Synchronization is used to ensure that only one thread has access to a resource at any
given moment.

The synchronization is mostly utilized for:

 To avoid thread interference.


 To avoid a consistency problem.

There are two kinds of synchronization.


1. Process Synchronization

2. Thread Synchronization

2.3.5.1 Process Synchronization: Process Synchronization is a mechanism for coordinating


the execution of several processes. It guarantees that the shared resources are secure and
orderly.

2.3.5.2 Thread Synchronization: Thread Synchronization coordinates and orders the


execution of threads in a multi-threaded program. There are two methods of thread
synchronization, as described below:

 Mutual Exclusive
 Cooperation (Inter-thread communication in Java)

Dr. Ratnesh Kumar Shukla Page 24


2.3.5.2.1 Mutual Exclusive: Mutual Exclusive helps to prevent threads from interfering with
one another while exchanging data. There are three types of mutual exclusives listed below:

 Synchronized method.
 Synchronized block.
 Static synchronization.

2.3.6 Inter-thread Communication: In Java, inter-thread communication is a mechanism


that allows another thread to enter (or lock) the same crucial region and be processed while a
thread is suspended.

Inter-thread communication is sometimes referred to as Cooperation in Java.

Polling refers to the frequent testing of a condition until it becomes true. Polling is often
implemented using loops to determine whether a specific condition is true or false. If the
statement is true, a certain action is executed. This costs numerous CPU cycles and renders
the implementation inefficient.

For example, in a classic queuing problem, one thread produces data while the other
consumes it.

To avoid polling, Java implements three methods: wait(), notify(), and notifyAll(). All of
these methods are final to the object class, ensuring that they are available to all classes. They
can only be utilized within synchronized blocks.

 wait(): Tells the calling thread to release the lock and sleep until another thread
accesses the same monitor and uses notify().
 notify(): It wakes up a single thread called wait() on the same object. Calling notify()
does not release a lock on a resource.
 notifyAll(): This function wakes up all threads that called wait() on the same object.

2.3.6.1 wait() method: The wait() method forces the current thread to relinquish the lock and
wait until another thread calls the notify() or notifyAll() methods on this object, or until a
certain period of time has passed.

The current thread must own this object's monitor, therefore it can only be called from the
synchronized method; otherwise, it will throw an exception.

Method Description
public final void wait()throws It waits until object is notified.
InterruptedException
public final void wait(long timeout)throws It waits for the specified amount of time.
InterruptedException

2.3.6.2 notify() method: The notify() method activates a single thread that is waiting on the
object's monitor. If any threads are waiting for this object, one of them is selected to be
awakened. The decision is arbitrary and made at the discretion of the implementation.

Dr. Ratnesh Kumar Shukla Page 25


Syntax:

public final void notify()

2.3.6.3 notifyAll() method: All threads waiting on the monitor of this object are woken up.

Syntax:

public final void notifyAll()

Understanding the process of inter-thread communication:

The point-by-point explanation for the above diagram is as follows:

1. Threads enter to obtain a lock.


2. On thread acquires the lock.
3. When you invoke the wait() method on the object, the thread enters a waiting state.
Otherwise, it releases the lock and leaves.
4. When you call the notify() or notifyAll() methods, the thread enters the notified state
(runnable).
5. Thread is now available to get the lock.
6. After completing the task, the thread releases the lock and exits the object's monitor
state.

Dr. Ratnesh Kumar Shukla Page 26

You might also like