Real Time Java : Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 8 SCHEDULING.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
0 Synchronization Problem Resource sharing –Requires mutual exclusion –Critical section A code section that should be executed mutually exclusively by.
CSE 522 Real-Time Scheduling (3)
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
UCDavis, ecs251 Fall /23/2007ecs251, fall Operating System Models ecs251 Fall 2007 : Operating System Models #3: Priority Inversion Dr. S.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Introduction to Embedded Systems
Real-Time Java Martin Schöberl. Real Time Java2 Overview What are real-time systems Real-time specification for Java RTSJ issues, subset Real-time profile.
Java Programming: Advanced Topics
111 © 2002, Cisco Systems, Inc. All rights reserved.
Enhancements to Java for Real Time Systems Theresa Dsena CSE Fall 2006 Prof. Ganesan.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
EEL Real-time Java part 2. EEL Acknowledgements All the lecture slides were adopted from the slides of Andy Wellings.
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
CSCI1600: Embedded and Real Time Software Lecture 18: Real Time Languages Steven Reiss, Fall 2015.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Thread Scheduling and Dispatching Maung Aung Han Marc E. Loy Jihua Zhong CIS 642: Seminar in Real-time Systems Instructor: Professor Insup Lee.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java Thread Programming
Deadlock and Starvation
Multithreading / Concurrency
Chapter 6: Process Synchronization
Scheduling and Resource Access Protocols: Basic Aspects
Deadlock and Starvation
Multithreaded Programming in Java
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall2014 9/20/2018.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Multithreading Chapter 23.
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall /27/2018.
G53SRP: Resource Sharing Issues
Multithreading.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Multithreading.
Multithreaded Programming
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CS333 Intro to Operating Systems
Threads and Multithreading
CSCI1600: Embedded and Real Time Software
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CIS 642: Real Time Java: Synchronization Reference http://www.rtj.org/ Core Java 2. Vol 2, Ch1 1/17/2019 CIS 642: Real Time Java: Synchronization