Java Socket Application

prettybadelyngeΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

56 εμφανίσεις



Java Socket Application

Distributed Systems IT332

Outline


Threads

in

Java


Multithreaded

Server

2

Questions


What

happen

after

the

server

sends

the

echo

message

to

the

client?



What

happen

if

two

clients

connect

to

the

server

at

the

same

time?


3

What is Multithreading?


Multithreading

is

similar

to

multi
-
processing
.


A

multi
-
processing

Operating

System

can

run

several

processes

at

the

same

time


Each

process

has

its

own

address/memory

space


The

OS's

scheduler

decides

when

each

process

is

executed


Only

one

process

is

actually

executing

at

any

given

time
.

However,

the

system

appears

to

be

running

several

programs

simultaneously


Separate

processes

to

not

have

access

to

each

other's

memory

space


Many

OSes

have

a

shared

memory

system

so

that

processes

can

share

memory

space


In

a

multithreaded

application,

there

are

several

points

of

execution

within

the

same

memory

space
.



Each

point

of

execution

is

called

a

thread


Threads

share

access

to

memory


4

Why use Multithreading
?


In

a

single

threaded

application,

one

thread

of

execution

must

do

everything


If

an

application

has

several

tasks

to

perform,

those

tasks

will

be

performed

when

the

thread

can

get

to

them
.


A

single

task

which

requires

a

lot

of

processing

can

make

the

entire

application

appear

to

be

"sluggish"

or

unresponsive
.



In

a

multithreaded

application,

each

task

can

be

performed

by

a

separate

thread


If

one

thread

is

executing

a

long

process,

it

does

not

make

the

entire

application

wait

for

it

to

finish
.


If

a

multithreaded

application

is

being

executed

on

a

system

that

has

multiple

processors,

the

OS

may

execute

separate

threads

simultaneously

on

separate

processors
.


5

What Kind of Applications Use
Multithreading
?


Any

kind

of

application

which

has

distinct

tasks

which

can

be

performed

independently


Any

application

with

a

GUI
.


Threads

dedicated

to

the

GUI

can

delegate

the

processing

of

user

requests

to

other

threads
.



The

GUI

remains

responsive

to

the

user

even

when

the

user's

requests

are

being

processed


Any

application

which

requires

asynchronous

response


Network

based

applications

are

ideally

suited

to

multithreading
.



Data

can

arrive

from

the

network

at

any

time
.



In

a

single

threaded

system,

data

is

queued

until

the

thread

can

read

the

data


In

a

multithreaded

system,

a

thread

can

be

dedicated

to

listening

for

data

on

the

network

port


When

data

arrives,

the

thread

reads

it

immediately

and

processes

it

or

delegates

its

processing

to

another

thread


6

Thread Support in
Java


Few

programming

languages

directly

support

threading


Although

many

have

add
-
on

thread

support


Add

on

thread

support

is

often

quite

cumbersome

to

use


The

Java

Virtual

machine

has

its

own

runtime

threads


Used

for

garbage

collection


Threads

are

represented

by

a

Thread

class


A

thread

object

maintains

the

state

of

the

thread


It

provides

control

methods

such

as

interrupt,

start,

sleep,

yield,

wait


When

an

application

executes,

the

main

method

is

executed

by

a

single

thread
.


If

the

application

requires

more

threads,

the

application

must

create

them
.


7

Thread
States



Threads

can

be

in

one

of

four

states


Created,

Running,

Blocked,

and

Dead



A

thread's

state

changes

based

on
:


Control

methods

such

as

start,

sleep,

yield,

wait,

notify


Termination

of

the

run

method



8

Created

Runnable

Blocked

Dead

start()

Thread()

run() method terminates

sleep()

wait()

notify()

How does a Thread run
?


The

thread

class

has

a

run()

method


run()

is

executed

when

the

thread's

start()

method

is

invoked


The

thread

terminates

if

the

run

method

terminates


To

prevent

a

thread

from

terminating,

the

run

method

must

not

end


run

methods

often

have

an

endless

loop

to

prevent

thread

termination


One

thread

starts

another

by

calling

its

start

method


The

sequence

of

events

can

be

confusing

to

those

more

familiar

with

a

single

threaded

model
.


9

Creating your own
Threads


The

obvious

way

to

create

your

own

threads

is

to

subclass

the

Thread

class

and

then

override

the

run()

method


This

is

the

easiest

way

to

do

it


It

is

not

the

recommended

way

to

do

it
.


Because

threads

are

usually

associated

with

a

task,

the

object

which

provides

the

run

method

is

usually

a

subclass

of

some

other

class


If

it

inherits

from

another

class,

it

cannot

inherit

from

Thread
.


The

solution

is

provided

by

an

interface

called

Runnable
.


Runnable

defines

one

method

-

public

void

run(
)


One

of

the

Thread

classes

constructor

takes

a

reference

to

a

Runnable

object


When

the

thread

is

started,

it

invokes

the

run

method

in

the

runnable

object

instead

of

its

own

run

method
.


10

Creating Threads

Two

ways

to

implement

threads
:


Option#
1


Create

a

class,

extend

Thread


Override

the

run()

method


Instantiate

the

class


Call

start()


Option#
2


Create

a

class,

implement

Runnable


Implement

the

run()

method


Instantiate

your

class


Instantiate

a

Thread

class,

pass

your

class

in

constructor


Call

start()

Creating Threads

Option

1
:

extending

class

thread

1
.

Create

a

class

by

extending

the

Thread

class

and

override

the

run()

method

class

MyThread

extends

Thread

{

public

void

run()

{

//

thread

body

of

execution

}}

2
.

Create

a

thread

object
:


MyThread

thr
1

=

new

MyThread
()
;

3
.

Start

Execution

of

created

thread
:


thr
1
.
start()
;

12

Creating Threads


Option

1
:

example




class

MyThread

extends

Thread

{


public

void

run()

{


System
.
out
.
println(“

this

thread

is

running

...

”)
;


}

}

class

ThreadEx
1

{


public

static

void

main(String

[]

args

)

{



MyThread

t

=

new

MyThread
()
;




t
.
start
()
;


}

}


13

Creating Threads


Option

2
:

implementing

Runnable
:

1
.

Create

a

class

that

implements

the

interface

Runnable

and

override

run()

method
:

class

MyThread

implements

Runnable

{



public

void

run()

{

//

thread

body

of

execution

}

}


2
.

Creating

Object
:


MyThread

myObject

=

new

MyThread
()
;


3
.

Creating

Thread

Object
:


Thread

thr
1

=

new

Thread(
myObject
)
;


4
.

Start

Execution
:


thr
1
.
start()
;

14

Creating Threads


Option

2
:

example




class

MyThread

implements

Runnable

{


public

void

run()

{



System
.
out
.
println(“

this

thread

is

running

...

”)
;


}

}

class

ThreadEx
2

{


public

static

void

main(String

[]

args

)

{



Thread

t

=

new

Thread(new

MyThread
())
;



t
.
start
()
;


}

}

15

Creating Multiple
Threads


The

previous

example

illustrates

a

Runnable

class

which

creates

its

own

thread

when

the

start

method

is

invoked
.


If

one

wished

to

create

multiple

threads,

one

could

simple

create

multiple

instances

of

the

Runnable

class

and

send

each

object

a

start

message


Each

instance

would

create

its

own

thread

object


Is

the

a

maximum

number

of

threads

which

can

be

created?


There

is

no

defined

maximum

in

Java
.


If

the

VM

is

delegating

threads

to

the

OS,

then

this

is

platform

dependent
.


A

good

rule

of

thumb

for

maximum

thread

count

is

to

allow

2
Mb

of

ram

for

each

thread


Although

threads

share

the

same

memory

space,

this

can

be

a

reasonable

estimate

of

how

many

threads

your

machine

can

handle
.


16

Single vs. Multi
-
threaded server


We

want

to

change

the

server

to

handle

two

clients

at

the

same

time
:


Use

threads!


The

idea

is

to

creates

a

new

thread,

when

a

client

sends

a

request
.

This

thread

is

responsible

to

satisfy

the

client

request
.

The

server

can

then

wait

for

new

client

requests

while

the

previously

created

thread

handles

the

request
.


17

Exercise


Step#
1
:

Compile

the

(echo

server

&

client)

and

execute

it

on

same

computer


Step#
2
:

Compile

the

(multithreaded

echo

server

&

client)

and

execute

it

on

same

computer


Step#
3
:

Compile

the

(multithreaded

echo

server

&

client)

and

execute

it

on

different

computers

18