Java 10 14
Java 10 14
PRACTICAL NO: 10
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.
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.
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:
2102271
Department of Information Technology
thread.start();
this.threadNumber = threadNumber;
try {
Thread.sleep(1000);
catch (InterruptedException e) {
e.printStackTrace();
2102271
Department of Information Technology
OUTPUT:
CONCLUSION:
2102271
Department of Information Technology
PRACTICAL NO: 11
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.
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.
CODE:
2102271
Department of Information Technology
thread1.start();
thread2.start();
super(name);
setPriority(priority);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
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
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:
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:
int[] numbers = { 1, 2, 3, 4, 5 };
try {
catch (ArrayIndexOutOfBoundsException e) {
2102271
Department of Information Technology
OUTPUT:
CONCLUSION:
PRACTICAL NO: 13
2102271
Department of Information Technology
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:
CODE:
int i = 1;
try {
2102271
Department of Information Technology
if (i > 5) {
} catch (MyCustomException e) {
super(message);
OUTPUT:
PRACTICAL NO: 14
2102271
Department of Information Technology
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 :
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;
OUTPUT:
2102271