Thread - Pi1

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

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

76 εμφανίσεις

Operating System

Java Multithreading Tutorial

Abdallah

Salama

(
Uni

Mannheim)

Multithreading Multitasking

Q

Please

COUNTER

COUNTER 1

COUNTER 2

Threads
and

Processes

(
Process
)


A

process

is
an instance of a program that is being executed



A
process

or several processes might be associated to a single
program.



P
rocesses
have separate address
spaces. So processes typically
don’t
share memory space
!



P
rocesses
interact only through system
-
provided inter
-
process
communication mechanisms.



A
thread

is a subset of the
process
(smaller set of instructions)



A
thread

is a light weight process:


executes within the context of the process


share the same resources allocated to the process.



Multiple threads
within a process share process state as well as
memory (address space) and other resources


Threads
and

Processes

(Thread)

Multithreading and Multiprocessing


Multithreading

is similar to
multiprocessing



A
multiprocessing

Operating System can run several processes at
the same
time on a single processor or multiprocessors.


Each process has its own address/memory space


The OS's scheduler decides when each process is
executed



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


The
VM or system’s scheduler
decides which thread executes at
any given time




Advantages
of

Multithreading


Keep a Process
Responsive



Keep a Processor
Busy



Take advantage
of parallel
processing




Resource sharing




Threads in Java



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
,
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


Threads in
Java
(Thread States)



Threads can be in one of four states


Created
,
Running
,
Blocked
, and
Dead




A thread's state changes
when:


O
ne of the control methods is called
such as
start
,
sleep
,
wait
,
notify


W
hen the run method
terminates


Created

Dead

Blocked

Runnable

Thread()

Start()

sleep()

wait()

notify()

run(
) terminates

Threads in Java
(
Creating
/
Running

a
thread
)

There

are

t
wo

techniques

to

create

a
thread

in Java:

1.
Implementing

the

Runnable

interface



The
Runnable

defines

a
single

method
,
run
()
.


The
run
()
method

is

meant

to

contain

the

code

executed

in
the

thread



The
Runnable

object is passed to the
Thread

constructor


2.
Extending

(
Subclass
)
Thread


Thread

class

itself implements
Runnable


An application can subclass a
Thread
.


An application can provide its own implementation of
run()



Creating
/
Running

a Thread
(
Example
)

Creating

a
thread

using

the

Runnable

Interface

Creating
/
Running

a Thread
(
Example
)

Creating

a
thread

by

subclass

Thread
class


Threads in Java
(
Synchroni
z
ation
)


Threads communicate primarily by
sharing access
to fields and the
objects reference fields refer to.



This
form of communication is extremely
efficient.



However
, it might introduce
thread interference
error



The
tool needed to prevent these errors is
synchronization
.

Synchronization

(Thread
Interference

Error)


Interference happens when two
operations, running in different
threads, but acting on the same
data



This means that the two operations
consist of multiple steps, and the
sequences of steps overlap
.



Counter

is designed so that each
invocation of increment will add 1
to
c, and subtract 1 for each
decrement.
However, if a
Counter

is
referenced from multiple threads,
interference between threads may
prevent this from happening as
expected
.

HOW?





Synchronization

(
Synchronized

Method
)

The Java programming language
provides a
synchronization idiom:
Synchronized
Method
by adding
synchronized

key word
in the signature of the method

If count is an instance of
SynchronizedCounter
, then making
these methods synchronized has
two effects
:



I
t
is not possible for two
invocations of synchronized
methods on the same object to
interleave



W
hen
a synchronized method
exits,

the changes
to the state of
the object
are guaranteed to be
visible
to all threads.




Synchronization

(
Semaphores
)


S
emaphores

are

a
way

to

synchronize

threads


What

will happen
if

release
()

method

has

been

forgotten
??

Synchronization

(Lock Objects)


Synchronized code relies on a simple kind of
reentrant
lock




only one thread can own a
lock
object at a time