Java Notes Unit 3
Java Notes Unit 3
UNIT 3
1) What is a thread? Describe the complete life cycle of thread.
A thread is the smallest unit of execution within a process. Threads share the same
memory space and resources within a process and can execute independently. They
are used to achieve concurrency, allowing multiple tasks to be performed
simultaneously within a single process.
The life cycle of a thread typically involves several stages:
New: A thread in the new state is created but hasn't started executing. It awaits
resources allocation by the OS.
Runnable: Threads in the runnable state are ready to execute but await CPU
allocation by the OS scheduler.
Running: In this state, a thread actively executes its code on the CPU, controlled
by the OS scheduler.
Dead: A thread enters the dead state after completing its execution or being
explicitly terminated, unable to be resumed.
Blocked: Threads in the blocked state are halted, awaiting certain conditions, such
as I/O completion or lock release, before resuming execution.
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Exception handling code
}
(iii) throw: The throw keyword is used to explicitly throw an exception within a code block.
It allows programmers to create custom exceptions or to throw predefined exceptions
when certain conditions are met. When a throw statement is encountered, the execution
of the current block is terminated, and the control is transferred to the nearest enclosing
try block or the appropriate catch block to handle the exception.
(iv) finally: The finally clause is used to define a block of code that will be executed
regardless of whether an exception is thrown or not. It is often used to perform cleanup
actions, such as releasing resources or closing files, that should be executed regardless of
whether an exception occurred. The finally block is executed after the try block and any
corresponding catch blocks have completed their execution, even if an exception was
thrown and caught or not caught.
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Exception handling code
} finally {
// Cleanup code that will always execute
}
package FirstPackage ;
// Class definition
public class Welcome {
// Method to display a message
public void display() {
System.out.println("This is the first program");
}
}
Cnntd--
// Main class
public class Main {
// Main method (driver method)
public static void main(String[] args) {
// Create an instance of Welcome class
Welcome obj = new Welcome();
4. java.net:
This package contains classes and interfaces for networking operations.
It enables communication over the Internet using protocols like TCP/IP and UDP.
Key classes include URL, URLConnection, Socket, and ServerSocket.
5. java.awt (Abstract Window Toolkit):
This package provides classes for creating graphical user interfaces (GUI) in Java.
It includes classes for creating windows, buttons, menus, and other GUI
components.
While it's been largely superseded by Swing and JavaFX for modern GUI
development, it still provides basic GUI functionality.
Notable classes include Frame, Button, Panel, and Graphics.
These predefined packages offer a rich set of functionality and are extensively used in Java
programming for various purposes, ranging from basic operations to advanced networking
and graphical user interface development.