23. Java processes
There are three types of Java program:
applications, applets, and servlets, all are written as
a class.
A Java application program has a main method, and is
run as an independent(standalone) process.
An applet does not have a main method, and is run
using a browser or the appletviewer.
A servlet does not have a main method, and is run in the
context of a web server.
A Java program is compiled into bytecode, a
universal object code. When run, the bytecode is
interpreted by the Java Virtual Machine (JVM).
24. Three Types of Java programs
Applications
a program whose byte code can be run on any system
which has a Java Virtual Machine. An application may
be standalone (monolithic) or distributed (if it interacts
with another process).
Applets
A program whose byte code is downloaded from a
remote machine and is run in the browser’s Java Virtual
Machine.
Servlets
A program whose byte code resides on a remote
machine and is run at the request of an HTTP client (a
browser).
25. Three Types of Java programs
c o m p u t e r
J a v a o b j e c t
J a v a V i r t u a l M a c h i n e
A s t a n d a l o n e J a v a a p p l i c a t i o n i s r u n o n a l o c a l m a c h i n e
J a v a o b je c t
J a v a V i r t u a l M a c h i n e
A n a p p l e t i s a n o b j e c t d o w n l o a d e d ( t r a n s f e r r e d ) f r o m a r e m o t e m a c h i n e ,
t h e n r u n o n a l o c a l m a c h i n e .
r e q u e s t
r e s p o n s e
a s e r v l e t
a n a p p l e t
A s e r v l e t i s a n o b je c t t h a t r u n s o n a r e m o t e m a c h i n e a n d
i n t e r a c t s w i t h a l o c a l p r o g r a m u s i n g a r e q u e s t - r e s p o n s e p r o t o c o l
a p r o c e s s
26. A sample Java application
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* A s a m p l e o f a s i m p l e J a v a a p p l i c a t i o n .
* M . L i u 1 / 8 / 0 2
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
i m p o r t ja v a . i o . * ;
c l a s s M y P r o g r a m {
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s )
t h r o w s I O E x c e p t i o n {
B u f f e r e d R e a d e r k e y b o a r d = n e w
B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( S y s t e m . i n ) ) ;
S t r i n g t h e N a m e ;
S y s t e m . o u t . p r i n t l n ( " W h a t i s y o u r n a m e ? " ) ;
t h e N a m e = k e y b o a r d . r e a d L i n e ( ) ;
S y s t e m . o u t . p r i n t ( " H e l l o " + t h e N a m e ) ;
S y s t e m . o u t . p r i n t l n ( " - w e l c o m e t o C S C 3 6 9 . n " ) ;
} / / e n d m a i n
} / / e n d c l a s s
27. A Sample Java Applet
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* A s a m p l e o f a s i m p l e a p p l e t .
* M . L i u 1 / 8 / 0 2
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
i m p o r t ja v a . a p p l e t . A p p l e t ;
i m p o r t ja v a . a w t . * ;
p u b l i c c l a s s M y A p p l e t e x t e n d s A p p l e t {
p u b l i c v o i d p a i n t ( G r a p h i c s g ) {
s e t B a c k g r o u n d ( C o l o r . b l u e ) ;
F o n t C l a u d e = n e w F o n t ( " A r i a l " , F o n t . B O L D , 4 0 ) ;
g . s e t F o n t ( C l a u d e ) ;
g . s e t C o l o r ( C o l o r . y e l l o w ) ;
g . d r a w S t r i n g ( " H e l l o W o r l d !" , 1 0 0 , 1 0 0 ) ;
} / / e n d p a i n t
} / / e n d c l a s s
< !- - A w e b p a g e w h i c h , w h e n b r o w s e d , w i l l r u n >
< !- - t h e M y A p p l e t a p p l e t >
< !- - M . L i u 1 / 8 / 0 2 >
< t i t l e > S a m p l e A p p l e t < / t i t l e >
< h r >
< a p p l e t c o d e = " M y A p p l e t . c l a s s " w i d t h = 5 0 0 h e i g h t = 5 0 0 >
< / a p p l e t >
< h r >
< a h r e f = " H e l l o . ja v a " > T h e s o u r c e . < / a >
28. A Sample Java Servlet
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* A s a m p l e o f a s i m p l e J a v a s e r v l e t .
* M . L i u 1 / 8 / 0 2
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
i m p o r t j a v a . i o . * ;
i m p o r t j a v a . t e x t . * ;
i m p o r t j a v a . u t i l . * ;
i m p o r t j a v a x . s e r v l e t . * ;
i m p o r t j a v a x . s e r v l e t . h t t p . * ;
p u b l i c c l a s s M y S e r v l e t e x t e n d s H t t p S e r v l e t {
p u b l i c v o i d d o G e t ( H t t p S e r v l e t R e q u e s t r e q u e s t ,
H t t p S e r v l e t R e s p o n s e r e s p o n s e )
t h r o w s S e r v l e t E x c e p t i o n , I O E x c e p t i o n {
P r i n t W r i t e r o u t ;
S t r i n g t i t l e = " M y S e r v l e t O u t p u t " ;
/ / s e t c o n t e n t t y p e a n d o t h e r r e s p o n s e h e a d e r
/ / f i e l d s f i r s t
r e s p o n s e . s e t C o n t e n t T y p e ( " t e x t / h t m l " ) ;
/ / t h e n w r i t e t h e d a t a o f t h e r e s p o n s e
o u t = r e s p o n s e . g e t W r i t e r ( ) ;
o u t . p r i n t l n ( " < H T M L > < H E A D > < T I T L E > " ) ;
o u t . p r i n t l n ( t i t l e ) ;
o u t . p r i n t l n ( " < / T I T L E > < / H E A D > < B O D Y > " ) ;
o u t . p r i n t l n ( " < H 1 > " + t i t l e + " < / H 1 > " ) ;
o u t . p r i n t l n ( " < P > H e l l o W o r l d !" ) ;
o u t . p r i n t l n ( " < / B O D Y > < / H T M L > " ) ;
o u t . c l o s e ( ) ;
} / / e n d d o G e t
} / / e n d c l a s s
29. Concurrent Processing
On modern day operating systems, multiple
processes appear to be executing concurrently
on a machine by timesharing resources.
P r o c e s s e s
t im e
P 1
P 2
P 3
P 4
T im e s h a r in g o f a r e s o u r c e
30. Concurrent processing within a process
It is often useful for a process to have parallel threads of
execution, each of which timeshare the system
resources in much the same way as concurrent
processes.
p a r e n t p r o c e s s
c h ild p r o c e s s e s
A p a r e n t p r o c e s s m a y s p a w n c h i l d p r o c e s s e s .
a p r o c e s s
m a in t h r e a d
c h ild t h r e a d 1
c h ild t h r e a d 2
A p r o c e s s m a y s p a w n c h i l d t h r e a d s
C o n c u r r e n t p r o c e s s i n g w i t h i n a p r o c e s s
31. Java threads
The Java Virtual Machine allows an application to have multiple
threads of execution running concurrently.
Java provides a Thread class:
public class Thread
extends Object
implements Runnable
When a Java Virtual Machine starts up, there is usually a single
thread (which typically calls the method named main of some
designated class). The Java Virtual Machine continues to
execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security
manager has permitted the exit operation to take place.
All threads have terminated, either by returning from the call to the
run method or by throwing an exception that propagates beyond
the run method.
Using a subclass of the Thread class
Using a class that implements the Runnable interface
32. Create a class that is a subclass of the Thread class
i m p o r t S o m e T h r e a d ;
p u b l i c c l a s s R u n T h r e a d s
{
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s )
{
S o m e T h r e a d p 1 = n e w S o m e T h r e a d ( 1 ) ;
p 1 . s t a r t ( ) ;
S o m e T h r e a d p 2 = n e w S o m e T h r e a d ( 2 ) ;
p 2 . s t a r t ( ) ;
S o m e T h r e a d p 3 = n e w S o m e T h r e a d ( 3 ) ;
p 3 . s t a r t ( ) ;
}
} / / e n d c l a s s R u n T h r e a d s
p u b l i c c l a s s S o m e T h r e a d e x t e n d s T h r e a d {
i n t m y I D ;
S o m e T h r e a d ( i n t i d ) {
t h i s . m y I D = i d ;
}
p u b l i c v o i d r u n ( ) {
i n t i ;
f o r ( i = 1 ; i < 1 1 ; i + + )
S y s t e m . o u t . p r i n t l n ( " T h r e a d " + m y I D + " : " + i ) ;
}
} / / e n d c l a s s S o m e T h r e a d
Declare a class to be a subclass of
Thread. This subclass should
override the run method of class
Thread. An instance of the subclass
can then be allocated and started:
33. Create a class that implements the Runnable interface
p u b l i c c l a s s R u n T h r e a d s 2
{
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s )
{
T h r e a d p 1 = n e w T h r e a d ( n e w S o m e T h r e a d 2 ( 1 ) ) ;
p 1 . s t a r t ( ) ;
T h r e a d p 2 = n e w T h r e a d ( n e w S o m e T h r e a d 2 ( 2 ) ) ;
p 2 . s t a r t ( ) ;
T h r e a d p 3 = n e w T h r e a d ( n e w S o m e T h r e a d 2 ( 3 ) ) ;
p 3 . s t a r t ( ) ;
}
}
c l a s s S o m e T h r e a d 2 i m p l e m e n t s R u n n a b l e {
i n t m y I D ;
S o m e T h r e a d 2 ( i n t i d ) {
t h i s . m y I D = i d ;
}
p u b l i c v o i d r u n ( ) {
i n t i ;
f o r ( i = 1 ; i < 1 1 ; i + + )
S y s t e m . o u t . p r i n t l n ( " T h r e a d " + m y I D + " : " + i ) ;
}
} / / e n d c l a s s S o m e T h r e a d
The other way to create a thread is to declare a class that
implements the Runnable interface. That class then
implements the run method. An instance of the class can
then be allocated, passed as an argument when creating
Thread, and started.
34. Thread-safe Programming
When two threads independently access and
update the same data object, such as a
counter, as part of their code, the updating
needs to be synchronized. (See next slide.)
Because the threads are executed concurrently,
it is possible for one of the updates to be
overwritten by the other due to the sequencing
of the two sets of machine instructions
executed on behalf of the two threads.
To protect against the possibility, a
synchronized method can be used to provide
mutual exclusion.
35. Race Condition
fe tc h v a lu e in c o u n te r a n d lo a d in to a r e g is te r
in c r e m e n t v a lu e in r e g is te r
s to r e v a lu e in r e g is te r to c o u n te r
tim e
fe tc h v a lu e in c o u n te r a n d lo a d in to a r e g is te r
in c r e m e n t v a lu e in r e g is te r
s to r e v a lu e in re g is te r to c o u n te r
in s tr u c tio n e x e c u te d in c o n c u r r e n t p r o c e s s o r th r e a d 1
in s t r u c t io n e x e c u te d in c o n c u r r e n t p r o c e s s o r th r e a d 2
T h i s e x e c u t i o n r e s u l t s i n t h e
v a l u e 2 i n t h e c o u n t e r
fe tc h v a lu e in c o u n te r a n d lo a d in to a r e g is te r
fe tc h v a lu e in c o u n te r a n d lo a d in to a r e g is te r
in c r e m e n t v a lu e in r e g is te r
in c r e m e n t v a lu e in r e g is te r
s to r e v a lu e in r e g is te r to c o u n te r
s to r e v a lu e in r e g is te r to c o u n te r
T h i s e x e c u t i o n r e s u l t s i n t h e
v a l u e 1 i n t h e c o u n t e r
36. Synchronized method in a thread
c l a s s S o m e T h r e a d 3 i m p l e m e n t s R u n n a b l e {
s t a t i c i n t c o u n t = 0 ;
S o m e T h r e a d 3 ( ) {
s u p e r ( ) ;
}
p u b l i c v o i d r u n ( ) {
u p d a t e ( ) ;
}
s t a t i c p u b l i c s y n c h r o n i z e d v o i d u p d a t e ( ) {
i n t m y C o u n t = c o u n t ;
m y C o u n t + + ;
c o u n t = m y C o u n t ;
S y s t e m . o u t . p r i n t l n ( " c o u n t = " + c o u n t +
" ; t h r e a d c o u n t = " + T h r e a d . a c t i v e C o u n t ( ) ) ;
}
}
54. The Domain Name System (DNS)
For user friendliness, each Internet address is
mapped to a symbolic name, using the DNS, in the
format of:
<computer-name>.<subdomain hierarchy>.<organization>.<sector name>{.<country code>}
e.g., www.csc.calpoly.edu.us
05/13/14 54
r o o t
c o m
e d u g o v n e t o r g m i l
o r g a n i z a t i o n
...
...
h o s t n a m e
t o p - l e v e l d o m a i n
s u b d o m a i n
i n t h e U . S .
T o p - l e v e l d o m a i n n a m e h a s t o b e a p p l i e d f o r .
S u b d o m a i n h i e r a c h y a n d n a m e s a r e a s s i g n e d
b y t h e o r g a n i z a t i o n .
c o u n t r y c o d e
55. The Domain Name System
For network applications, a domain name
must be mapped to its corresponding Internet
address.
Processes known as domain name system
servers provide the mapping service, based
on a distributed database of the mapping
scheme.
The mapping service is offered by thousands
of DNS servers on the Internet, each
responsible for a portion of the name space,
called a zone. The servers that have access
to the DNS information (zone file) for a zone is
said to have authority for that zone.
56. Top-level Domain Names
.com: For commercial entities, which anyone, anywhere in the
world, can register.
.net : Originally designated for organizations directly involved
in Internet operations. It is increasingly being used by
businesses when the desired name under "com" is already
registered by another organization. Today anyone can register
a name in the Net domain.
.org: For miscellaneous organizations, including non-profits.
.edu: For four-year accredited institutions of higher learning.
.gov: For US Federal Government entities
.mil: For US military
Country Codes :For individual countries based on the
International Standards Organization. For example, ca for
Canada, and jp for Japan.