Applet as JAVA Application

21
Java Programming Applet as Java Applications Prof. Shailesh Gahane Assistant Professor Dr. D. Y. Patil School of MCA Charholi (Bk), Lohegaon, Pune – 412105 Mail ID: [email protected] Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

description

Applet As Java Applications 2.1 Introduction 2.2 Applet Life Cycle 2.3 Applets specific methods & Related HTML references 2.4 An Applet Skeleton 2.5 The HTML APPLET Tag with all attributes. 2.6 Creating an Applet 2.7 Displaying it using Web Browser, appletwiewer.exe 2.8 Passing parameters to applet 2.9 Advantages and Disadvantages of Applet Vs Applications

Transcript of Applet as JAVA Application

Page 1: Applet as JAVA Application

Java ProgrammingApplet as

Java ApplicationsProf. Shailesh Gahane

Assistant ProfessorDr. D. Y. Patil School of MCA

Charholi (Bk), Lohegaon, Pune – 412105Mail ID: [email protected]

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Page 2: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

“An applet is a special kind of java program that is primarily used in Internet computing”.

Local Applet and Remote AppletWe 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 computer.For local applets we doesn’t required any internet connection.Similarly, The remote Applets which are downloaded / developed from / into the remote machine and embed it into web page. It requires a Internet connection.

Page 3: Applet as JAVA Application

Some restrictions related to applet programming:1) Applets can not read or write to the file systems. 2) Applets can not communicate with any other server than the one in which they were stored originally.3) Means Applet can not run any program on the System.

Steps to Develop and Test the Applet:Build an Applet code(.java file)Compile the Applet(.class file)Design a web page using HTML Tag (Create HTML file)

Preparing an <APPLET> tagHost an <APPLET> tag into the web pageTest the Applet code

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Page 4: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods that load and play audio clips.java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Panel

java.awt.Applet

Page 5: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Page 6: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

init( ) : The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

start( ) : The start( ) method is called after init( ). It

is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen.

paint( ) : The paint( ) method is called each time

your applet’s output must be redrawn. Whenever the applet must redraw its

output, paint( ) is called. The paint( ) method has one parameter of type Graphics.

Page 7: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

stop( ) The stop( ) method is called when a web browser leaves the HTML document containing the Applet

destroy( ) The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.

Page 8: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Ex: Step1: Write SimpleApplet.java

import java.awt.*; import java.applet.*;public class SimpleApplet extends Applet{ public void paint(Graphics g) { g.drawString("Wellcome To the World of Applet Programming", 40, 50); g.drawString("A Simple Applet", 20, 20); } }

Step 2: Write SimpleApplet.html<HTML> <HEAD> <TITLE> MY FIRST APPLET PROGRAM</TITLE> </HEAD> <BODY> <APPLET CODE=SimpleApplet.class HEIGHT=400 WIDTH= 600></APPLET> </BODY> </HTML>

Page 9: Applet as JAVA Application

Java ProgrammingMCA II SEM IV

Unit 7Multithreading in Java

Programming

Prof. Shailesh GahaneAssistant Professor

Dr. D. Y. Patil School of MCACharholi (Bk), Lohegaon, Pune - 412105Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Page 10: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Multithreading in Java Programming

• Java provides built-in support for multithreaded programming.

• A multithreaded 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. Thus, multithreading is a specialized form of multitasking.

Two Types of Multitasking:1) Process Based2) Thread Based

Page 11: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

DIFFERENCE BETWEEN PROCESS BASED AND THREAD BASED MULTITASKING

Two or more than two programs execute concurrently. For eg. Typing in MS-WORD and listening the song. Where as Single program perform two or more task simultaneously. For eg. we will saving the file and order to print the file.

More Overhead compared to Thread Based Multitasking.

Running Heavyweight tasks. Eg. Run the Java compiler at the same time that you are using a text editor, Where as running the Lightweight tasks. Eg: text editor can format text at the same time that it is printing.

It has separate address space Where as Thread Based Multitasking shares the address space.

Inter-process communication is expensive and limited Where as Inter-thread communication is inexpensive. Switching between one thread to another is easy.

Page 12: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

T H R E A D S T A T E S / M O D E L A N D L I F E C Y C L E

New

Ready Running

In ActiveFinishe

d

start ()

stop()

run ()

stop ()

stop()

sleep () / suspend ()

wakeup (), notify () notify

all ()

Page 13: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread States or Life Cycle

New State:- When any kind of thread is newly created it enters into the new state. New state tell us that the lifetime of the thread starts and thread is considered as alive.

Ready State:- This state is also called as Ready to Run state. Mainly thread is primarily entered into this state after the start() method is invoked.

Running State:- When the thread pass from ready position to the execution that means it is entered into the Running State.

Page 14: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Inactive State:- There are several reasons for the thread to enter into the Inactive state. The meaning of this state is that the thread is entered into the non runnable state. Sleep() or wait() methods are responsible.

Finished State:- This is the final state of the thread life cycle. This state is also called as Dead State. After the completion of run() method or the stop() method is invoked then the finished state comes into the picture.

Page 15: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread PrioritiesThere are priorities for some of the methods which work on the thread priority.SetPriority() Priorities range from 0 to 10. MIN_PRIORITY(0), NORM_PRIORITY(5), MAX_PRIORITY(10)

GetPriority() : This method is used to get the priority of the thread.Thread Methods• IsAlive(): returns a true or false.• join(): this method waits until the thread on

which it terminates. Its name comes from the concept of calling thread waiting until the specified thread joins it.

• suspend(): pauses the execution.• resume(): restart the execution.• destroy(): kill the method.

Page 16: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

• activeCount(): returns the number of active thread in a thread group.

• interrupt(): Interrupts the thread.• isDaemon(): returns true or false and checks the

thread is Daemon or not.• yield(): used for temporarily pause and allows

further thread to execute.

Page 17: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread Synchronization in Java

• Java Thread Synchronization uses• the concept of monitors• the concept that every object

has a lock• Thread synchronization is divided in two

parts• Synchronized Method• Synchronized Block / Statement

Page 18: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

• Every object has a lock associated with it.• Calling a synchronized method attempts to

possess the lock– If no one uses the lock, then the thread uses the

lock.

• If any thread wants to execute the synchronized method, firstly it has to obtain the object lock. If the lock is already held by another thread, then calling thread has to wait.

• The lock is released when a thread exits the synchronized method.

Page 19: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

public synchronized void enter(Object item){

while (count == BUFFER_SIZE)Thread.yield();

++count;buffer[in] = item;in = (in + 1) % BUFFER_SIZE;

}

Example of Synchronized Method

Synchronized Block:In synchronized statements, we have to specify the object that provides the intrinsic lock.The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.The general form of Synchronized block is:Synchronized (object reference expression){Block of code;}

Page 20: Applet as JAVA Application

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Example of Synchronized Block:

synchronized (this) { for(int i=0; i<5; i++) {

System.out.println("Started...."+str);

try {

Thread.sleep(5000); } catch(Exception e1)

{} } }

Example of Synchronized method:

public synchronized void display(String str)

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

System.out.println("Started...."+str);

try { Thread.sleep(5000); } catch(Exception e1){} } }}

Page 21: Applet as JAVA Application

Thank you !!!

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Prof. Shailesh GahaneAssistant Professor

Dr. D. Y. Patil School of MCACharholi (Bk), Lohegaon, Pune – 412105Mail ID: [email protected]