0% found this document useful (0 votes)
10 views13 pages

Java 10 14

Uploaded by

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

Java 10 14

Uploaded by

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

Department of Information Technology

PRACTICAL NO: 10

AIM: Write a program to implement multithreading in java.

Software Requirement: Operating system, Java Development Kit (JDK), Java Runtime
Environment (JRE).

Hardware Requirement: Processor (clock speed of 1 GHz or higher), RAM (at least
512 mb), Hard Disk (200-500mb for jdk and jre installation).

THEORY:

Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process.

Threads can be created by using two mechanisms :

1. Extending the Thread class


2. Implementing the Runnable Interface

 Thread creation by extending the Thread class

We create a class that extends the java.lang.Thread class. This class overrides the run()
method available in the Thread class. A thread begins its life inside run() method. We create
an object of our new class and call start() method to start the execution of a thread. Start()
invokes the run() method on the Thread object.

 Thread creation by implementing the Runnable Interface

We create a new class which implements java.lang.Runnable interface and override run()
method. Then we instantiate a Thread object and call start() method on this object.

CODE:

public class MultithreadingExample {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

Thread thread = new Thread(new MyRunnable(i));

2102271
Department of Information Technology

thread.start();

static class MyRunnable implements Runnable {

private int threadNumber;

public MyRunnable(int threadNumber) {

this.threadNumber = threadNumber;

public void run() {

try {

System.out.println("Thread " + threadNumber + " is running.");

Thread.sleep(1000);

System.out.println("Thread " + threadNumber + " has finished.");

catch (InterruptedException e) {

e.printStackTrace();

2102271
Department of Information Technology

OUTPUT:

CONCLUSION:

Multithreading is a way to perform multiple tasks simultaneously, taking advantage of multi-


core processors, but it can introduce complexities that need to be carefully managed,
especially when dealing with shared resources and synchronization. the provided Java
program demonstrates multithreading by creating and running 12 threads concurrently. Each
thread simulates work by printing messages and introducing a 1-second delay using
Thread.sleep(1000).

2102271
Department of Information Technology

PRACTICAL NO: 11

AIM: Write a program to implement priority in multithreading.

Software Requirement: Operating system, Java Development Kit (JDK), Java Runtime
Environment (JRE).

Hardware Requirement: Processor (clock speed of 1 GHz or higher), RAM (at least
512 mb), Hard Disk (200-500mb for jdk and jre installation).

THEORY:

Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process.

Threads can be created by using two mechanisms :

1. Extending the Thread class


2. Implementing the Runnable Interface

Priorities in threads is a concept where each thread is having a priority which in layman’s
language one can say every object is having priority here which is represented by numbers
ranging from 1 to 10.

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:

 public static int NORM_PRIORITY


 public static int MIN_PRIORITY
 public static int MAX_PRIORITY

CODE:

2102271
Department of Information Technology

public class PriorityExample {

public static void main(String[] args) {

Thread thread1 = new MyThread("Thread 1", Thread.MAX_PRIORITY);

Thread thread2 = new MyThread("Thread 2", Thread.MIN_PRIORITY);

thread1.start();

thread2.start();

static class MyThread extends Thread {

public MyThread(String name, int priority) {

super(name);

setPriority(priority);

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(getName() + " is running. Iteration: " + i);

try {

sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

System.out.println(getName() + " has finished.");

2102271
Department of Information Technology

OUTPUT:

CONCLUSION:

The program demonstrated that we can set thread priorities to influence thread scheduling,
with higher-priority threads having a better chance of running first. However, thread priority
is not a guarantee and can be system-dependent. It should be used judiciously, and other
synchronization mechanisms are often more reliable for controlling thread execution order in
multithreaded applications.

PRACTICAL NO: 12

2102271
Department of Information Technology

AIM: Write a program to perform Exception handling in java.

Software Requirement: Operating system, Java Development Kit (JDK), Java Runtime
Environment (JRE).

Hardware Requirement: Processor (clock speed of 1 GHz or higher), RAM (at least
512 mb), Hard Disk (200-500mb for jdk and jre installation).

THEORY:

Exception is an unwanted or unexpected event, which occurs during the execution of a


program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
Exceptions can be caught and handled by the program. When an exception occurs within a
method, it creates an object. This object is called the exception object. It contains information
about the exception, such as the name and description of the exception and the state of the
program when the exception occurred.

Types of Exceptions

2102271
Department of Information Technology

Java defines several types of exceptions that relate to its various class libraries. Java also
allows users to define their own exceptions.

CODE:

public class Example {

public static void main(String[] args) {

int[] numbers = { 1, 2, 3, 4, 5 };

try {

int element = numbers[6];

System.out.println("Element: " + element);

catch (ArrayIndexOutOfBoundsException e) {

System.err.println("An ArrayIndexOutOfBoundsException occurred: " +


e.getMessage());

System.out.println("Program continues after exception handling.");

2102271
Department of Information Technology

OUTPUT:

CONCLUSION:

Exception handling is a fundamental practice in software development that enables the


graceful handling of unexpected errors or exceptional situations during program execution. It
serves to prevent program crashes, provides diagnostic information for debugging, enhances
program robustness, and offers user-friendly error messages and actions. Exception handling
also ensures proper resource cleanup and controlled program flow, making it an essential
component for creating stable and reliable software applications.

PRACTICAL NO: 13

2102271
Department of Information Technology

AIM: Write a program to perform Exception handling in java.

Software Requirement: Operating system, Java Development Kit (JDK), Java Runtime
Environment (JRE).

Hardware Requirement: Processor (clock speed of 1 GHz or higher), RAM (at least
512 mb), Hard Disk (200-500mb for jdk and jre installation).

THEORY:

Exception is an unwanted or unexpected event, which occurs during the execution of a


program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
Exceptions can be caught and handled by the program. When an exception occurs within a
method, it creates an object. This object is called the exception object. It contains information
about the exception, such as the name and description of the exception and the state of the
program when the exception occurred.

CODE:

public class CustomException {

public static void main(String[] args) {

int i = 1;

try {

2102271
Department of Information Technology

if (i > 5) {

throw new MyCustomException("Value of 'i' exceeds the limit (5).");

} catch (MyCustomException e) {

System.err.println("Custom Exception Caught: " + e.getMessage());

static class MyCustomException extends Exception {

public MyCustomException(String message) {

super(message);

OUTPUT:

PRACTICAL NO: 14

2102271
Department of Information Technology

AIM: Write a program to demonstrate the use of Applet in java.

Software Requirement: Operating system, Java Development Kit (JDK), Java Runtime
Environment (JRE).

Hardware Requirement: Processor (clock speed of 1 GHz or higher), RAM (at least
512 mb), Hard Disk (200-500mb for jdk and jre installation).

THEORY:

An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works at client side. An applet is embedded in an HTML page using the
APPLET or OBJECT tag and hosted on a web server. Applets are used to make the website
more dynamic and entertaining.

Important points :

 All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.


 Applets are not stand-alone programs. Instead, they run within either a web browser
or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
 In general, execution of an applet does not begin at main() method.
 Output of an applet window is not performed by System.out.println(). Rather it is
handled with various AWT methods, such as drawString().

Life cycle of an applet :

It is important to understand the order in which the various methods shown in the above
image are called. When an applet begins, the following methods are called, in this sequence:

2102271
Department of Information Technology

 init( )
 start( )
 paint( )
When an applet is terminated, the following sequence of method calls takes place:
 stop( )
 destroy( )

CODE:
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet


{

public void paint(Graphics g)


{
g.drawString("Hello World", 20, 20);
}

OUTPUT:

2102271

You might also like