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

Unit 4 Multi Threading and Applet

• Introduction • Multithreading in java • java.lang.Thread • Main Thread • Creation of new Threads • Thread State in java • Thread Priority • Multithreading using isAlive() and join( ) • Applet • Introduction • Applet class • Applet Structure • Example Applet Program • Applet Life Cycle • Common methods used in displaying the output • paint(), update() and repaint() methods • More about applet tag • Methods of Graphics class o drawLine(), drawArc(), fillArc(), drawOval(), fillOval(), drawPolygon

Uploaded by

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

Unit 4 Multi Threading and Applet

• Introduction • Multithreading in java • java.lang.Thread • Main Thread • Creation of new Threads • Thread State in java • Thread Priority • Multithreading using isAlive() and join( ) • Applet • Introduction • Applet class • Applet Structure • Example Applet Program • Applet Life Cycle • Common methods used in displaying the output • paint(), update() and repaint() methods • More about applet tag • Methods of Graphics class o drawLine(), drawArc(), fillArc(), drawOval(), fillOval(), drawPolygon

Uploaded by

Khushi Pal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Multi

Threading
and Applets
Multi Threading
Multithreading in Java is a process of executing multiple threads
simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing.


Multiprocessing and multithreading, both are used to achieve
Multitasking
multitasking.

Multitasking is a process of executing multiple tasks simultaneously. We


use multitasking to utilize the CPU. Multitasking can be achieved in two
ways:

● Process-based Multitasking (Multiprocessing)


● Thread-based Multitasking (Multithreading)
What is Thread in Java?
A thread is a lightweight
subprocess, the smallest unit of
processing. It is a separate path
of execution.

Threads are independent. If there


occurs exception in one thread, it
doesn't affect other threads. It
uses a shared memory area.
Main Thread in Java
Java provides built-in support for
multithreaded programming. A multi-
threaded program contains two or more
parts that can run concurrently. Each part
of such a program is called a thread, and
each thread defines a separate path of
execution.

When a Java program starts up, one


thread begins running immediately. This
is usually called the main thread of our
program because it is the one that is
executed when our program begins.
How to create a Thread in Java ?
There are two ways to create a thread:

1. By extending Thread class


2. By implementing Runnable interface.
Java Thread Example by Extending Thread Class
FileName: Multi.java
Java Thread Example by Implementing Runnable Interface
FileName: Multi3.java
Thread Scheduler in Java
A component of Java that decides
which thread to run or execute and
which thread to wait is called a
thread scheduler in Java.

There are some criteria that decide


which thread will execute first.
There are two factors for scheduling
a thread i.e. Priority and Time of
arrival.
Thread Sleep in Java - on the custom thread
Thread Sleep in Java - on the main thread
Can we Start Thread Twice ?
No. After starting a thread, it can never be started again. If you does so, an
IllegalThreadStateException is thrown. In such case, thread will run once but for
second time, it will throw exception.
What if we call Java run() method directly instead start() method?

● Each thread starts in a separate call stack.


● Invoking the run() method from the main thread, the run() method goes onto the
current call stack rather than at the beginning of a new call stack.
Output:

running...
What if we call Java run() method directly instead start() method?

● Each thread has a priority.

● Priorities are represented by a number between 1 and 10.


In most cases, the thread scheduler schedules the threads
according to their priority (known as preemptive
scheduling).

● But it is not guaranteed because it depends on JVM


specification that which scheduling it chooses.
Setter & Getter Method of Thread Priority

● Let's discuss the setter and getter method of the thread


priority.
● public final int getPriority(): The
java.lang.Thread.getPriority() method returns the priority of
the given thread.
● public final void setPriority(int newPriority): The
java.lang.Thread.setPriority() method updates or assign the
priority of the thread to newPriority.
The method throws IllegalArgumentException if the value
newPriority goes out of the range, which is 1 (minimum) to
10 (maximum).
3 constants defined in Thread class:

1. public static int MIN_PRIORITY


2. public static int NORM_PRIORITY
3. public static int MAX_PRIORITY
Thread Priority Example
Thread Priority Example
Thread Priority Example
Thread Priority Example - Output
JAVA Applets
JAVA PROGRAMS – FLAVOR’S

APPLICATION APPLET
(Console Application, Desktop (Embed into a Webpage)
Application)
WHAT IS APPLICATION ?
❖ It is similar to all other kind of
Programs like in C,C++, PERL etc.
to solve an problem.
❖Java Application is just like a Java
program that runs on an
underlying Operating System with
the support of a virtual machine. It
is also known as an Application
Program.
❖The Graphical User Interface is not
necessary to execute the java
applications, it can be run WITH or
WITHOUT it.
EXAMPLES OF JAVA APPLICATIONS

Graphics Ex -3
Graphics Ex -1 FREE Graphics Ex -2 STUDENT RECORD STORAGE SYSTEM
CELL GAME SWING ART (DATABASE EXAMPLE)
EXAMPLES OF JAVA APPLICATIONS

Ex #1
• Robotino Festo - Labyrinth (Java)
• https://www.youtube.com/watch?v=A6R4gcXKywo

Ex #2
• My Java Applications
• https://www.youtube.com/watch?v=pWMf9BLoIdo

Ex #3
• Sweet Home 3D
• http://www.sweethome3d.com/
BROWSER

WHAT IS APPLET ?
APPLET

❖ 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 web site more dynamic and
entertaining.
HOW
APPLET
LOOKS
LIKE ?
LOCAL APPLETS
❖ We can Create our own applet by own
design and Embed them into web pages.
❖Local applets are DEVELOPED in a
single system and it is STORED in a
local system.
❖ The web page will SEARCH the local
system directories, FIND the local
applet and EXECUTE it.
❖ Execution of local applets Doesn't
required any INTERNET connection.
REMOTE APPLETS

❖ An Remote Applet is that which is DEVELOPED by someone else


and STORED on a remote computer CONNECTED to the
INTERNET.
APPLET LIFE CYCLE
WORKING OF APPLET LIFE CYCLE
IT IS FOLLOWED BY PAINT()
IT IS EXECUTED WHEN APPLET IS STOPPED OR
BROWSER WINDOW IS MINIMIZED.

INIT()
IT IS FOLLOWED BY PAINT()
WHEN APPLET IS IT IS EXECUTED WHEN APPLET IS STOPPED OR
LOADED BROWSER WINDOW IS MINIMIZED.
FOR THE FIRST TIME
START() STOP()
STOP(
)
IT IS FOLLOWED BY INIT() DESTROY()
IT IS USED TO START
APPLET PAINT(
)
PAINT() START()
IT IS USED TO DESTROY
MEMORY OCCUPIED BY APPLET.
IT IS FOLLOWED BY START()
IT IS EXECUTED APPLET WHEN BROWSER WINDOW IT IS INVOKED ONCE IN IT’S
IT IS USED TO DRAW
IS MAXIMIMED AGAIN. ENTIRE LIFE CYCLE.
ANY TEXT OR SHAPE ON APPLET
INIT() METHOD

Variable
It is the first declaration
It is Executed
method to and
on One Time in
Execute when initialization
It’s Entire Life
the Applet is operations are
Cycle.
Executed. performed in
this method.
STAGE 1
START() METHOD

It also executes
whenever the
The start() applet is
The start()
method contains restored,
method executes
the actual code maximized or
immediately after
of the applet that moving from one
the init() method.
should run. tab to another
tab in the
browser.
STAGE 2
PAINT() METHOD

The paint()
method executes
The paint()
after the
method is used
execution It can be
to redraw the
of start() method executed N
output on the
and whenever Number of Times.
applet display
the applet or
area.
browser is
resized.
STAGE 3
STOP() METHOD

The stop()
method executes
The stop() when the applet
It can be
method stops the is minimized or
executed N
execution of the when moving
Number of Times.
applet. from one tab to
another in the
browser.
STAGE 4
DESTROY() METHOD

The destroy()
method executes
stop() method
when the applet The destroy()
executes just It can be executed
window is closed method removes
before when Only 1 Time in It’s
or when the tab the applet object
destroy() method Entire Life Cycle.
containing the from memory
is invoked.
webpage is
closed.

STAGE 5
APPLET LIFE CYCLE PRACTICAL
❖ Step - 1 :: Importing the Required Package(s) for Applet to Run.

❖ Here we will import 2 different Package(s) Namely : AWT and APPLET


❖ “awt” Package is Used to get the Class Named Graphics.
❖ “applet” Package is Used to get the Class Named Applet.
APPLET LIFE CYCLE PRACTICAL
❖ Step - 2 :: Defining <APPLET> Tag.

❖ Here we will define <APPLET> Tag in Comment because this file will be Compiled
by JAVA Compiler and Java Compiler won’t Understand the HTML Tags.
❖ But Still we will need to define that tag to Provide information to the Applet
Viewer.
❖ In Applet Tag we will need to define 3 Different Attributes.
❖ Code :: This Attribute contains information of the class file that has to be
read to load Applet in the Applet Viewer.
❖ Width :: This Attribute contains information of the Width of the Applet
Window that will be Displayed.
❖ Height :: This Attribute contains information of the Height of the Applet
Window that will be Displayed.
APPLET LIFE CYCLE PRACTICAL
❖ Step - 3 :: Creating an Applet Class.

❖ Here we will Create a class with Any Name and Extend it with the “APPLET”
class to create an Applet and Differentiate it will Normal Class.
❖ Note :: Your class file must be “PUBLIC” because its Object is created by Java
Plugin software that resides on the browser.
APPLET LIFE CYCLE PRACTICAL
❖ Step - 4 :: Defining Every
Method of an Applet
Class.

❖ Here we will Override the 4


Different Methods Provided
by “APPLET” Class.
❖ Methods which comes from
Applet class are as follows.
❖ public void init()
❖ public void start()
❖ public void stop()
❖ public void destroy()
APPLET LIFE CYCLE PRACTICAL
❖ Step - 5 :: Defining Paint Method of an Component Class.

❖ Here we will Override the Paint method provided by “COMPONENT” Class to Draw
some Graphics(It might be any Text or any Shape) on the Screen.
❖ Methods which comes from COMPONENT class are as follows.
❖ public void paint(Graphics G) – Here paint is been overridden from
COMPONENT Class to draw something on Screen using “GRAPHICS”(Imported
from JAVA.AWT Package) class Object Named “G”.
HOW TO RUN APPLET PROGRAM
❖ Step – 1 :: Open CMD from the
Location where your APPLET
Program is Created. (As Shown
in the GIF)

❖ Step – 2 :: Compile your APPLET


Program using “javac” (Java
Compiler).

❖ Step – 3 :: Run your Applet


using “AppletViewer”
Command (To view an Applet).
OUTPUT APPLET LIFE CYCLE
PROGRAM
Scan this QR CODE for :

Full Program Source Code


Access
METHOD EXECUTION
SEQUENCE
The Method Execution Sequence when an Applet is EXECUTED:
⮚ init()
⮚ start()
⮚ paint()

The Method Execution Sequence when an Applet is CLOSED:


⮚ stop()
⮚ destroy()
GRAPHICS - DRAW STRING
EXAMPLE
(X,Y) = GUESS THIS (X,Y) =
(0,0) CO- (200,75)
ORDINATES

{
Work APPLET VIEWER
Area

75)
Y – AXIS (0-
Assume that the Applet X – AXIS (0-
Window Size is of 200)
Width = 200
Height =75
OUTPUT DRAW STRING
PROGRAM
Scan this QR CODE for :

Y = 250
X = 125

(X,Y) = (125,250)

Full Program Source Code


Access
GRAPHICS - DRAW LINE
EXAMPLE
(X1,Y1) = (X2,Y2) =
(100,100) (200,100)

GUESS THE CO-ORDINATES OF BLUE LINE

Work

{
APPLET VIEWER
Area
Applet Window
Size is of
Width = 200
Height =75
OUTPUT DRAW STRING
PROGRAM
Scan this QR CODE for :

Y2 = 100
Y1 = 100
X2 =
200
X1 = 100

(X1,Y1) (X2,Y2)
(100,100) (200,100)
Note :: Only Width of the Line is
Changing,
Height for Both the Points is Same So
there will No change in Y1 and Y2 Co- Full Program Source Code
ordinates. Access
GRAPHICS - DRAW RECT
EXAMPLE
(X,Y) = So this will
(20,20) be the
4th Point

{
Work APPLET VIEWER
Area
Applet Window WIDTH = 100
Size is of HEIGHT =
Width = 200 100
Height =75
OUTPUT DRAW RECT
PROGRAM
Scan this QR CODE for :

(X,Y)
(20,20) WIDTH = 100
HEIGHT = 100

NOTE :: If the Height and Width of the


RECTANGLE is Same, then it will draw SQUARE.

Full Program Source Code


Access
GRAPHICS - FILL RECT
EXAMPLE
(X,Y) = So this will
(20,20) be the
4th Point

{
Work APPLET VIEWER

HEIGHT = 100
Area WIDTH =
Applet Window 100
Size is of
Width = 200
Height =75
OUTPUT FILL RECT PROGRAM
Scan this QR CODE for :

(X,Y)
WIDTH =
(20,20) 100
100
HEIGHT =

NOTE :: If the Height and Width of the


RECTANGLE is Same, then it will draw SQUARE.

Full Program Source Code


Access
GRAPHICS - DRAW OVAL
EXAMPLE

X = 20 Y = 20

{
APPLET VIEWER
Work
Area

100
HEIGHT =
WIDTH =
100
Applet Window Size is
of
Width = 200
Height =75
OUTPUT DRAW OVAL
PROGRAM
Scan this QR CODE for :

Y = 20

HEIGHT = 100
X = 20 WIDTH = 100

Full Program Source Code


Access
GRAPHICS - FILL OVAL
EXAMPLE
X = 20 Y = 20

Work

{
APPLET VIEWER
Area

100
HEIGHT =
WIDTH =
100
Applet Window
Size is of
Width = 200
Height =75
OUTPUT FILL OVAL
PROGRAM
Scan this QR CODE for :

Y = 20
100
HEIGHT =
X=
20

WIDTH =
100

Full Program Source Code


Access
ADVANTAGES OF APPLET
As applet is a Small Java Program, so it is platform independent
code which is capable to run on any browser.

Applets Cache Quickly, that’s why there will be no latency in


downloading of them

It can also be resort as a Real Time Application.

They increase Interactivity for users.

Various small tasks such as performing Login, Inventory Checking,


Task Scheduling can be done by applets running over Intranets.
DRAW BACKS OF APPLETS
Some browsers especially Mobile Browsers which are running on Apple IOS
or Android don’t support applets run.

Applets don’t access client-side resources, i.e., ( Files And Operating


system).

Applet can only Extract Information about client-machine is its name, java
version, OS

Applets have to tag as Trusted Applets and they must be registered to APS
(Applet Security Manager)

Applet cannot work with Native Methods.


Thank You !!

You might also like