Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real Time Java : Synchronization

Similar presentations


Presentation on theme: "Real Time Java : Synchronization"— Presentation transcript:

1 Real Time Java : Synchronization
Maung Aung Han, Marc Loy Jihua Zhong Seminor in Real-time Systems Instructor: Professor Insup Lee 1/17/2019 CIS 642

2 CIS 642: Real Time Java: Synchronization
Real time Java issues Threads Release characteristics & failures Scheduling Synchronization Time and timers Memory management Asynchronous event handling Asynchronous transfer of control Exceptions System-level options 1/17/2019 CIS 642: Real Time Java: Synchronization

3 Multitasking – Multithread
Multitasking(operating system related) Cooperative(non-preemptive) Preemptive Multiple process Each process has own variables Multithread Threads share the same data 1/17/2019 CIS 642: Real Time Java: Synchronization

4 CIS 642: Real Time Java: Synchronization
Multithread Extremely useful in practice browser deal with multiple hosts Open window or view another page while downloading data Java use a thread to do garbage collection in the background Very complicated !! 1/17/2019 CIS 642: Real Time Java: Synchronization

5 Example: without Synchronization
From Core java 2 unsync.html 1/17/2019 CIS 642: Real Time Java: Synchronization

6 Example : possible output
Transactions:10000 Sum: Transactions:20000 Sum: Transactions:30000 Sum: Transactions:40000 Sum: Transactions:50000 Sum: Transactions:60000 Sum: Transactions:70000 Sum: Transactions:80000 Sum: Transactions:90001 Sum: Transactions: Sum: Transactions: Sum: Transactions: Sum: Transactions: Sum: 94792 Transactions: Sum: 94792 Transactions: Sum: 94792 1/17/2019 CIS 642: Real Time Java: Synchronization

7 CIS 642: Real Time Java: Synchronization
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. 1/17/2019 CIS 642: Real Time Java: Synchronization

8 CIS 642: Real Time Java: Synchronization
Why Synchronization? Unsynchronized Synchronized Thread 1 Thread 2 Thread 1 Thread 2 Time transfer transfer transfer transfer 1/17/2019 CIS 642: Real Time Java: Synchronization

9 CIS 642: Real Time Java: Synchronization
How to Synchronize? For Regular Java Threads Object Locks Key word : synchronized unlock object lock 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 1/17/2019 CIS 642: Real Time Java: Synchronization

10 Example: with Synchronization
From Core java 2 sunc.html 1/17/2019 CIS 642: Real Time Java: Synchronization

11 RT Issues: Synchronization
Risk of unbounded priority inversions Priorities can uncover or existing race conditions Need well defined thread and locking semantics running outside block blocked at guard waiting (blocked) on a condition variable synchronized block running inside block priority key: high middle low 1/17/2019 CIS 642: Real Time Java: Synchronization

12 CIS 642: Real Time Java: Synchronization
RT Threads Realtime Thread NoHeapRealtimeThread 1/17/2019 CIS 642: Real Time Java: Synchronization

13 Semantics & requirements
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 1/17/2019 CIS 642: Real Time Java: Synchronization

14 Semantics & requirements (contd.)
Priority Inversion Avoidance 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. The priority inheritance protocol must be implemented by default and can be overridden Support the priority ceiling emulation protocol 1/17/2019 CIS 642: Real Time Java: Synchronization

15 Semantics & requirements (contd.)
Priority Inversion Avoidance 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, non-blocking, shared access to objects access by both regular Java threads and NoHeapRealtimeThreads 1/17/2019 CIS 642: Real Time Java: Synchronization

16 Semantics & requirements (contd.)
Determinism 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 1/17/2019 CIS 642: Real Time Java: Synchronization

17 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) 1/17/2019 CIS 642: Real Time Java: Synchronization

18 Priority ceiling emulation protocol (highest locker protocol)
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 1/17/2019 CIS 642: Real Time Java: Synchronization

19 Implementation: Monitor Control
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 1/17/2019 CIS 642: Real Time Java: Synchronization

20 Implementation: PriorityCeilingEmulation
Objects under the influence of this protocol have the effect that a thread entering the monitor has its effective priority- for priority-based 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 1/17/2019 CIS 642: Real Time Java: Synchronization

21 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 1/17/2019 CIS 642: Real Time Java: Synchronization

22 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 public boolean blockingWrite(java.lang.Object object) //full public boolean force(java.lang.Object object) public java.lang.Object nonblockingRead() public java.lang.Object nonblockingWrite() SYN UNSYN 1/17/2019 CIS 642: Real Time Java: Synchronization

23 Implementation: WaitFreeReadQueue
Problem: synchronizated access objects shared between real-time threads and regular java threads might cause the real-time threads to incur delays due to execution of the garbage collector 1/17/2019 CIS 642: Real Time Java: Synchronization

24 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 1/17/2019 CIS 642: Real Time Java: Synchronization

25 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) 1/17/2019 CIS 642: Real Time Java: Synchronization

26 CIS 642: Real Time Java: Synchronization
Summary Synchronization is very important in real world and complicated for implementation Efficiency for real time java are less discussed 1/17/2019 CIS 642: Real Time Java: Synchronization

27 CIS 642: Real Time Java: Synchronization
Reference Core Java 2. Vol 2, Ch1 1/17/2019 CIS 642: Real Time Java: Synchronization


Download ppt "Real Time Java : Synchronization"

Similar presentations


Ads by Google