Real Time Java Synchronization

Download Report

Transcript Real Time Java Synchronization

Real Time Java :
Synchronization
Maung Aung Han,
Marc Loy
Jihua Zhong
Seminor in Real-time Systems
Instructor: Professor Insup Lee
7/26/2016
CIS 642
1
Real time Java issues










7/26/2016
Threads
Release characteristics & failures
Scheduling
Synchronization
Time and timers
Memory management
Asynchronous event handling
Asynchronous transfer of control
Exceptions
System-level options
CIS 642: Real Time Java:
Synchronization
2
Multitasking – Multithread

Multitasking(operating system
related)
Cooperative(non-preemptive)
 Preemptive


Multiple process


Multithread

7/26/2016
Each process has own variables
Threads share the same data
CIS 642: Real Time Java:
Synchronization
3
Multithread
Extremely useful in practice

browser deal with multiple hosts


Open email window or view another
page while downloading data
Java use a thread to do garbage
collection in the background
Very complicated !!
7/26/2016
CIS 642: Real Time Java:
Synchronization
4
Example:
without Synchronization
From Core java 2
unsync.html
7/26/2016
CIS 642: Real Time Java:
Synchronization
5
Example : possible output
Transactions:10000 Sum: 100000
Transactions:20000 Sum: 100000
Transactions:30000 Sum: 100000
Transactions:40000 Sum: 100000
Transactions:50000 Sum: 100000
Transactions:60000 Sum: 100000
Transactions:70000 Sum: 100000
Transactions:80000 Sum: 100000
Transactions:90001 Sum: 100000
Transactions:100000 Sum: 100000
Transactions:110000 Sum: 100000
Transactions:120000 Sum: 100000
Transactions:130000 Sum: 94792
Transactions:140000 Sum: 94792
Transactions:150000 Sum: 94792
…
7/26/2016
CIS 642: Real Time Java:
Synchronization
6
Why Synchronization?
Race condition!
 Two or more threads access to
same object and each call a
method that modifier the state of
the object. Corrupted object result!
So synchronization needed in the
some method.
7/26/2016
CIS 642: Real Time Java:
Synchronization
7
Why Synchronization?
Unsynchronized
Thread 1
Synchronized
Thread 2
Thread 1
Thread 2
transfer
Time
transfer
transfer
transfer
7/26/2016
CIS 642: Real Time Java:
Synchronization
8
How to Synchronize?
For Regular Java Threads

Object Locks


unlock object lock


7/26/2016
Key word : synchronized
Wait method: Called inside
synchronized method and the
current thread is blocked and put
into wait list
Notify / NotifyAll method: remove
the thread from wait list
CIS 642: Real Time Java:
Synchronization
9
Example:
with Synchronization

From Core java 2

7/26/2016
sunc.html
CIS 642: Real Time Java:
Synchronization
10
RT Issues: Synchronization

running outside block
blocked at guard
synchronized block

waiting (blocked) on
a condition variable
running inside block
priority key:
7/26/2016
high
middle

Risk of unbounded
priority inversions
Priorities can uncover
or existing race
conditions
Need well defined
thread and locking
semantics
low
CIS 642: Real Time Java:
Synchronization
11
RT Threads

Realtime Thread

7/26/2016
NoHeapRealtimeThread
CIS 642: Real Time Java:
Synchronization
12
Semantics & requirements
7/26/2016

Priority
 Loosely (not strict)

Waite Queues
 Thread waiting to enter synchronization block
are in execution eligibility(EE) order
 Blocked thread that becomes ready to run
 Explicitly set by itself or other threads
 Thread performs a yield
 Preempted with a thread with higher EE
 FIFO for threads with same EE
CIS 642: Real Time Java:
Synchronization
13
Semantics & requirements (contd.)

Priority Inversion Avoidance
1. Any conforming implementation must provide
an implementation of the synchronized
primitive with default behavior that ensure that
there is no unbounded priority inversion.This
must apply to code running with both the
implementation and real-time threads.
2. The priority inheritance protocol must be
implemented by default and can be overridden
3.
Support the priority ceiling emulation protocol
7/26/2016
CIS 642: Real Time Java:
Synchronization
14
Semantics & requirements (contd.)

Priority Inversion Avoidance
4.
NoHeapRealtimeThreads have an implicit execution
eligibility that must be higher than that of the
garbage collector , but regular java threads never
does.
Note that: if the execution of NoHeapRealtimeThreads must not
be delayed by the execution of garbage collector, it is
impossible for a NoHeapRealtimeThread to synchronize on
an object accessed by regular Java Threads. RTSJ provides
three wait-free queue classes to provide protected, nonblocking, shared access to objects access by both regular
Java threads and NoHeapRealtimeThreads
7/26/2016
CIS 642: Real Time Java:
Synchronization
15
Semantics & requirements (contd.)

Determinism


7/26/2016
Conforming implementations shall provide a
fixed upper bound on the time required to enter
a synchronized block for an unlocked monitor
Clearly document the behavior of the algorithm
which provide the monitor control
CIS 642: Real Time Java:
Synchronization
16
Priority inheritance protocol
(default)
If the thread t1 attempts to acquire
a lock held by a lower-priority
thread t2, the priority of t2 is raised
to that of t1 as long as t2 holds the
lock ( recursively if t2 is itself
waiting to acquire a lock held by a
even lower priority thread)
7/26/2016
CIS 642: Real Time Java:
Synchronization
17
Priority ceiling emulation protocol
(highest locker protocol)



7/26/2016
A monitor is given a priority ceiling when it is
created
when a thread enters synchronized code, its priority
raised to the monitor’s ceiling priority, thus ensuring
mutually exclusive access to the code since it will
not be preempted by any thread that could possibly
attempt to enter the same monitor
If, through programming error, a thread has a higher
priority than the ceiling of the monitor it is attempting
to enter, then an exception is thrown
CIS 642: Real Time Java:
Synchronization
18
Implementation:
Monitor Control
MonitorControl
PriorityCeilingEmulation
PriorityInheritance
public abstract class MonitorControl {
public MonitorControl()
public static void setMonitorControl(MonitorControl policy)
//control the default monitor behavior for object monitors used
//by synchronized statement and methods in the system
public static void setMonitorControl(java.lang.Object monitor,
MonitorControl policy)
//policy only affect the indicated object policy
7/26/2016
CIS 642: Real Time Java:
Synchronization
19
Implementation:
PriorityCeilingEmulation
Objects under the influence of this protocol have the effect that a
thread entering the monitor has its effective priority- for prioritybased dispatching-raised to the ceiling on the entry, and is restored to
its previous effective priority when it exits the monitor.
public class PriorityCeilingEmulation extends MonitorControl
public PriorityCeilingEmulation(int ceiling)
// ceiling – Priority ceiling value
public int getDefaultCeiling()
//get the priority ceiling for an object
7/26/2016
CIS 642: Real Time Java:
Synchronization
20
Implementation:
PriorityInheritance
a thread entering the monitor will boost the effective priority of the
thread in the monitor to its own effective priority. And will be restored
to its previous effective priority when it exits the monitor.
public class PriorityInheritance extends MonitorControl
public PriorityInheritance()
// ceiling – Priority ceiling value
public static PriorityInheritance instance()
//return a pointer to the singleton PriorityInheritance
7/26/2016
CIS 642: Real Time Java:
Synchronization
21
Implementation:
WaitFreeDequeue
The wait-free queue classes facilitate communication and
synchronization between instances of RealtimeThread and
java.lang.Thread
public class WaitFreeDequeue
public WaitFreeDequeue(java.lang.Thread writer,
java.lang.Thread reader, int maximum, MemoryArea area)
public java.lang.Object blockingRead() //when queueis empty
SYN
public boolean blockingWrite(java.lang.Object object) //full
public boolean force(java.lang.Object object)
UN public java.lang.Object nonblockingRead()
SYN public java.lang.Object nonblockingWrite()
7/26/2016
CIS 642: Real Time Java:
Synchronization
22
Implementation:
WaitFreeReadQueue

7/26/2016
Problem:
 synchronizated access objects
shared between real-time
threads and regular java
threads might cause the realtime threads to incur delays
due to execution of the garbage
collector
CIS 642: Real Time Java:
Synchronization
23
Implementation:
WaitFreeReadQueue
public class WaitFreeReadQueue
public WaitFreeReadQueue(java.lang.Thread writer,
java.lang.Thread reader, intmaximum, MemoryArea memory)
public WaitFreeReadQueue(java.lang.Thread writer,
java.lang.Thread reader, intmaximum, MemoryArea memory,
boolean notify)
public void clear()
public boolean isEmpty()
public boolean isFull()
public java.lang.Object Read()
public int size()
public void waitForData()
public synchronized boolean write(java.lang.Object object)
//this call blocks on queue full and will wait until there is space in the queue
7/26/2016
CIS 642: Real Time Java:
Synchronization
24
Implementation:
WaitFreeWriteQueue
public class WaitFreeWriteQueue
public WaitFreeWriteQueue(java.lang.Thread writer,
java.lang.Thread reader, intmaximum, MemoryArea memory)
public WaitFreeWriteQueue(java.lang.Thread writer,
java.lang.Thread reader, intmaximum, MemoryArea memory,
boolean notify)
public void clear()
public boolean isEmpty()
public boolean isFull()
public java.lang.Object Read() // block on the queue empty
public int size()
public void waitForData()
public synchronized boolean write(java.lang.Object object)
7/26/2016
CIS 642: Real Time Java:
Synchronization
25
Summary


7/26/2016
Synchronization is very important
in real world and complicated for
implementation
Efficiency for real time java are
less discussed
CIS 642: Real Time Java:
Synchronization
26
Reference


7/26/2016
http://www.rtj.org/
Core Java 2. Vol 2, Ch1
CIS 642: Real Time Java:
Synchronization
27