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

Java Notes Unit 3

SYBscIt mumbai unviresity

Uploaded by

kobawag832
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java Notes Unit 3

SYBscIt mumbai unviresity

Uploaded by

kobawag832
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________

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.

Prof. Bhakti Chaudhari 1


SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________
2) Explain following clause w.r.t. exception handling: (i) try (ii) catch (iii) throw (iv) finally
(i) try: The try clause is used to enclose the code block where exceptions might occur. It's
followed by one or more catch or finally blocks. Within the try block, if any exception
occurs, the control is transferred to the catch block or finally block (if present) depending
on the type of exception and the available exception handlers.
try {
// Code that may throw an exception
}
(ii) catch: The catch clause is used to handle exceptions that are thrown within the
corresponding try block. It allows specific handling of different types of exceptions. When
an exception occurs in the try block, the control is transferred to the catch block that
matches the type of the thrown exception, and the appropriate code inside the catch block
is executed.

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.

throw new ExceptionType("Exception message");

(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
}

Prof. Bhakti Chaudhari 2


SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________
3) How do we design and implement packages in Java?
A package in Java is a mechanism used to organize and group related classes and interfaces
together, providing a namespace to avoid naming conflicts and promoting code reusability
and maintainability.
There are two types of packages in java:

 User-defined Package (Create Your Own Package’s)


 Built-in packages are packages from the java application programming interface that
are the packages from Java API for example such as swing, util, net, io, AWT, lang,
javax, etc.
Declare a Package:
To declare the name of the package to be created. The package statement simply
defines in which package the classes defined belong.

package FirstPackage ;

Implementation: To Create a Class Inside A Package


First Declare The Package Name As The First Statement Of Our Program.
Then We Can Include A Class As A Part Of The Package.

// Name of package to be created


package FirstPackage;

// Class in which the above created package belong to


// Name of package to be created
package FirstPackage;

// Class definition
public class Welcome {
// Method to display a message
public void display() {
System.out.println("This is the first program");
}
}

Cnntd--

Prof. Bhakti Chaudhari 3


SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________
To use this package, you can create another Java file in the same directory or in a different
directory and import the Welcome class from the FirstPackage package:

// Importing the Welcome class from the FirstPackage package


import FirstPackage.Welcome;

// 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();

// Call the display method


obj.display();
}
}

Prof. Bhakti Chaudhari 4


SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________
4) What is package in Java? State and explain any Five prede0ined packages in
Java.
A package in Java is a mechanism used to organize and group related classes and interfaces
together, providing a namespace to avoid naming conflicts and promoting code reusability
and maintainability.
Five commonly used predefined packages in Java:
1. java.lang:
 This package is automatically imported into every Java program.
 It contains fundamental classes and interfaces that are essential for basic Java
programming, such as String, Object, System, Math, and Throwable.
 Classes in this package do not require an explicit import statement.
2. java.util:
 This package contains utility classes and interfaces for working with collections,
date and time, random numbers, and other utility functions.
 It includes classes like ArrayList, HashMap, Date, Calendar, and Random.
 Commonly used subpackages include java.util.concurrent for concurrency
utilities and java.util.stream for functional-style operations on streams of
elements.
3. java.io:
 This package provides classes for input and output operations.
 It includes classes for reading and writing data to files, streams, and other I/O
devices.
 Important classes include File, InputStream, OutputStream, Reader, and Writer.

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.

Prof. Bhakti Chaudhari 5


SYIT SEM IV

Course Name: Java Programming


___________________________________________________________________________
5) Explain following methods related to threads: 1)suspend ( ) 2) resume ( ) 3)
yield ( ) 4) wait ( )
1. suspend():
 Pauses the execution of a thread temporarily.
 Deprecated due to potential deadlocks and thread safety issues.
 Can lead to thread starvation if not used carefully.
2. resume():
 Resumes the execution of a thread previously suspended.
 Deprecated for similar reasons as suspend(), as it can lead to thread safety problems.
 Can result in unpredictable behavior if not used correctly.
3. yield():
 Allows the currently executing thread to pause temporarily, giving other threads of
the same priority a chance to run.
 Does not guarantee that other threads will run immediately.
 It's a hint to the scheduler, and actual behavior may vary between different Java
Virtual Machine (JVM) implementations.
4. wait():
 Pauses the current thread's execution until another thread notifies it to continue.
 Must be called within a synchronized block or method to avoid errors.
 Helps in building efficient inter-thread communication mechanisms.

Prof. Bhakti Chaudhari 6

You might also like