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

3rd Unit Java

The document discusses key concepts in Java, including the 'finally' block for resource management, the Thread Life Cycle with its various states, and handling exceptions using nested try-catch blocks. It also covers thread priorities, explaining how they influence thread scheduling and execution. Each section includes Java program examples and highlights the advantages of the discussed features.

Uploaded by

silentkillerrblx
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 views9 pages

3rd Unit Java

The document discusses key concepts in Java, including the 'finally' block for resource management, the Thread Life Cycle with its various states, and handling exceptions using nested try-catch blocks. It also covers thread priorities, explaining how they influence thread scheduling and execution. Each section includes Java program examples and highlights the advantages of the discussed features.

Uploaded by

silentkillerrblx
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

1.

Write a JAVA program for finally block

Definition
The finally block in Java is used to execute important code such as
closing files or releasing resources. It is executed whether an exception
occurs or not.

Syntax

Java Program
Output

Exception Caught: [Link]: / by zero

Finally block is always executed.

Advantages

●​ Executes regardless of exception occurrence.


●​ Used for resource cleanup.
●​ Improves program reliability.
●​ Commonly used for closing files, database connections, and
streams.
2. Write short notes on Thread Life Cycle and write a Java program for
creating threads

Thread Life Cycle


A thread is a lightweight process that executes independently within a program. During its
execution, it passes through different states known as the Thread Life Cycle.

Thread States

1.​ New (Born)


○​ Thread object is created but start() is not called.
2.​ Runnable (Ready)
○​ After calling start(), the thread is ready to run and waits for CPU allocation.
3.​ Running
○​ The thread is executing its run() method.
4.​ Blocked/Waiting
○​ The thread waits for another thread or resource to become available.
5.​ Terminated (Dead)
○​ The thread completes execution or is stopped.

Java Program for Creating Threads


Using Thread Class

Output
Thread is running...
Advantages of Threads
●​ Enables multitasking.
●​ Improves CPU utilization.
●​ Executes multiple tasks simultaneously.
●​ Increases application responsiveness.























How to handle multiple catch blocks for a nested try
block? Explain with an example.

Introduction
Exception handling is a mechanism in Java used to handle runtime errors and maintain the
normal flow of program execution. A nested try block is a try block placed inside another
try block, and multiple catch blocks are used to handle different types of exceptions
separately.

Nested Try Block


A nested try block is useful when a section of code inside a try block may generate a
different exception that requires separate handling.

Syntax
Java Program

Output
Arithmetic Exception: [Link]: / by zero
Array Index Exception: [Link]: Index 5 out of bounds
for length 3

Explanation
●​ The inner try block generates an ArithmeticException, which is handled by its
corresponding catch block.
●​ After handling the first exception, execution continues in the outer try block.
●​ Accessing an invalid array index generates an
ArrayIndexOutOfBoundsException, which is handled by the outer catch block.
●​ If any other exception occurs, the general Exception catch block handles it.
Advantages
●​ Handles different exceptions independently.
●​ Improves program readability and maintainability.
●​ Prevents abnormal program termination.
●​ Allows better organization of exception handling code.
●​ Increases the reliability and robustness of Java applications.
4)Write a program to explain thread priorities usage

Introduction
In Java, thread priority is used to indicate the importance of a thread. The thread scheduler
generally gives preference to threads with higher priority over those with lower priority.
Thread priorities range from 1 to 10, where 1 is the minimum priority, 5 is the default
priority, and 10 is the maximum priority.

Thread Priority Constants


●​ Thread.MIN_PRIORITY = 1
●​ Thread.NORM_PRIORITY = 5
●​ Thread.MAX_PRIORITY = 10

The methods used are:

●​ setPriority(int priority) – Sets the priority of a thread.


●​ getPriority() – Returns the current priority of a thread.

Java Program
Sample Output
Thread-1 Priority: 1
Thread-2 Priority: 10

Note: The execution order may vary depending on the JVM and operating system scheduler.

Explanation
●​ Three threads are created with different priorities.
●​ Thread-1 is assigned minimum priority (1).
●​ Thread-2 is assigned maximum priority (10).
●​ The scheduler generally gives preference to higher-priority threads, but the execution
order is not guaranteed.

Advantages
●​ Improves CPU scheduling efficiency.
●​ Gives preference to important tasks.
●​ Enhances application performance and responsiveness.
●​ Supports better resource utilization in multithreaded programs.
●​ Makes task execution more organized.

You might also like