Concurrent, Real-Time and Distributed Programming in Java

Similar documents
Network Performance Analysis

The Internet of Things

What is a thread anyway?

1 OBJECT-ORIENTED PROGRAMMING 1

Performance Throughput Utilization of system resources

NETWORKS AND TELECOMMUNICATIONS SERIES. LTE Services. Jean-Gabriel Remy Charlotte Letamendia

Contents. 6-1 Copyright (c) N. Afshartous

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Robotics and Autonomous Systems

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Multithreaded Programming

Java Programming Language Advance Feature

Unit - IV Multi-Threading

ABOUT CORE JAVA COURSE SCOPE:

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Object Oriented Programming (II-Year CSE II-Sem-R09)

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Java Threads. COMP 585 Noteset #2 1

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Essential Series. Springer-Verlag London Ltd.

Graph Theory and Applications

Introduction to Java

Java Threads. Introduction to Java Threads

FOCUS SERIES. Baidu SEO. Challenges and Intricacies of Marketing in China. Véronique Duong

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Real-Time Java. Martin Schöberl

MODERN MULTITHREADING

Chapter 16. Layering a computing infrastructure

Advanced programming for Java platform. Introduction

CMSC 132: Object-Oriented Programming II. Threads in Java

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Java s Implementation of Concurrency, and how to use it in our applications.

Remote Method Invocation

Virtual Machine Design

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Java Threads. Written by John Bell for CS 342, Spring 2018

CPS221 Lecture: Threads

Threads in Java (Deitel & Deitel)

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Unit III Rupali Sherekar 2017

RPC and RMI. 2501ICT Nathan

7. MULTITHREDED PROGRAMMING

Multi-threaded programming in Java

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Basics of. Multithreading in Java

Definition: A thread is a single sequential flow of control within a program.

Concurrent Programming

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

User Space Multithreading. Computer Science, University of Warwick

Concurrent Programming using Threads

Multithreading Pearson Education, Inc. All rights reserved.

Introduction to Java Threads

CS11 Java. Fall Lecture 7

CS193k, Stanford Handout #12. Threads 4 / RMI

Threads Chate Patanothai

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

1 Shyam sir JAVA Notes

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Multi-threading in Java. Jeff HUANG

Handling Multithreading Approach Using Java Nikita Goel, Vijaya Laxmi, Ankur Saxena Amity University Sector-125, Noida UP India

Concurrent Computing CSCI 201 Principles of Software Development

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

CS 556 Distributed Systems

Module - 4 Multi-Threaded Programming

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Operating System Support

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

Multithreading using Java. Dr. Ferdin Joe John Joseph

Distributed Programming with RMI. Overview CORBA DCOM. Prepared By: Shiba R. Tamrakar

CSCD 330 Network Programming

Programming Language Concepts: Lecture 11

Multithread Computing

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

REMOTE METHOD INVOCATION INTRODUCTION TO RMI, A JAVA API FOR RPC-STYLE INVOCATION OF REMOTE OBJECT METHODS

Concurrent Object-Oriented Programming

Remote Procedure Call

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Real-time & Embedded Systems Workshop July 2007 Building Successful Real-time Distributed Systems in Java

public class Shared0 { private static int x = 0, y = 0;

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Model Requirements and JAVA Programs MVP 2 1

Processes and Threads

Message Passing vs. Distributed Objects. 5/15/2009 Distributed Computing, M. L. Liu 1

Multithreaded Programming

Java Training For Six Weeks

Transcription:

Concurrent, Real-Time and Distributed Programming in Java

FOCUS SERIES Jean-Charles Pomerol Concurrent, Real-Time and Distributed Programming in Java Threads, RTSJ and RMI Badr Benmammar

First published 2018 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc. Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address: ISTE Ltd John Wiley & Sons, Inc. 27-37 St George s Road 111 River Street London SW19 4EU Hoboken, NJ 07030 UK USA www.iste.co.uk www.wiley.com ISTE Ltd 2018 The rights of Badr Benmammar to be identified as the author of this work have been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. Library of Congress Control Number: 2017957888 British Library Cataloguing-in-Publication Data A CIP record for this book is available from the British Library ISSN 2051-2481 (Print) ISSN 2051-249X (Online) ISBN 978-1-78630-258-8

Contents List of Acronyms.... Introduction... vii ix Chapter 1. Introduction to Threads in Java... 1 1.1. Processes versus threads... 1 1.2. Concurrent computing... 2 1.3. Thread creation... 3 1.4. Types of thread... 4 1.5. Monotask versus multitask... 5 1.6. Different states of a thread... 13 1.7. Lifecycle of a thread... 13 1.8. A few notes concerning threads... 16 1.8.1. Two threads without using sleep... 16 1.8.2. Time allocation between two threads... 17 1.8.3. Priority between threads... 19 1.9. Programming a task: Timer and TimerTask... 21 1.9.1. By specifying an initial delay... 21 1.9.2. With an initial delay and periodicity... 23 Chapter 2. Thread Synchronization... 27 2.1. Synchronization upon termination: join() method... 27 2.2. Resource in mutual exclusion: synchronized modifier... 30 2.3. Shared variables: internal class... 33 2.4. The problem with mutual exclusions... 35 2.5. Synchronized block... 36 2.6. Synchronized instance method... 41 2.7. Shared variables: class variable... 43

vi Concurrent, Real-Time and Distributed Programming in Java 2.8. Synchronization between threads... 45 2.8.1. Wait and notifyall... 45 2.8.2. Wait and notify... 48 2.9. Classic Producer Consumer pattern... 51 2.10. Semaphore in Java... 54 2.10.1. Before Java 1.5... 55 2.10.2. After Java 1.5... 57 Chapter 3. Real-Time Systems and Real-Time Java... 61 3.1. Real-time systems... 61 3.1.1. Definition... 61 3.1.2. Examples of real-time operating systems... 62 3.1.3. Types of real-time... 62 3.1.4. Architecture... 63 3.1.5. Task ordinance with priorities... 63 3.2. Java in real-time... 65 3.2.1. RTSJ (Real-Time Specification for Java)... 65 3.2.2. Implementations... 67 Chapter 4. Distributed Programming in Java... 71 4.1. Definition of a distributed application... 71 4.2. Communication in a distributed application... 72 4.2.1. Low-level communication: socket... 72 4.2.2. High-level communication: middleware... 89 Appendix... 127 Bibliography... 155 Index... 157

List of Acronyms API CNI CORBA DCOM DGC FIFO GC GCC GCJ GNU IIOP IP J2SE J2EE J2ME Application Programming Interface Cygnus Native Interface Common Object Request Broker Architecture Distributed Component Object Model Distributed Garbage Collection First In, First Out Garbage Collector GNU Compiler Collection GNU Compiler for Java GNU s Not Unix Internet Inter-ORB Protocol Internet Protocol Java 2 Standard Edition Java 2 Enterprise Edition Java 2 Micro Edition

viii Concurrent, Real-Time and Distributed Programming in Java JDK JNI Java SE Development Kit Java Native Interface JRMP JSSE JVM KVM OSI PCP PIP RMI RMIC RPC RRL RTSJ SSL TCP TLS UDP URL WinCE Java Remote Method Protocol Java Secure Socket Extension Java Virtual Machine Kilo VM Open Systems Interconnection Priority Ceiling Protocol Priority Inheritance Protocol Remote Method Invocation RMI Compiler Remote Procedure Call Remote Reference Layer Real-Time Specification for Java Secure Sockets Layer Transmission Control Protocol Transport Layer Security User Datagram Protocol Uniform Resource Locator Windows Embedded Compact

Introduction This book constitutes an introduction to real-time and distributed concurrent computing, using Java object-oriented language as a support tool for describing algorithms. It describes in particular the synchronization mechanisms (in cooperation and in competition) and data-sharing mechanisms (internal class, static type variables) between threads in Java. We then discuss the use of Java for real-time applications. Subsequently, a presentation of RTSJ (Real-Time Specification for Java) is also introduced in this book. Finally, a presentation of distributed computing can also be found. We focus in particular on low-level communication using TCP Sockets and high-level communication using Java RMI (Remote Method Invocation) middleware. The book also contains an appendix including a set of practical application exercises in relation to the theme of the book. Knowledge of Java language is a prerequisite to properly understanding this book.

1 Introduction to Threads in Java 1.1. Processes versus threads The operating system is tasked with allocating the necessary resources (memory, processing time, inputs/outputs) to the processes and ensuring they do not interfere with one another (isolation) [TAN 01]. This principle is illustrated in the following example in which variable a is defined in two different classes. When executing both classes, the two allocated memory zones for this variable are completely isolated. Figure 1.1. Isolation between processes Concurrent, Real-Time and Distributed Programming in Java: Threads, RTSJ and RMI, First Edition. Badr Benmammar. ISTE Ltd 2018. Published by ISTE Ltd and John Wiley & Sons, Inc.

2 Concurrent, Real-Time and Distributed Programming in Java Most operating systems offer a distinction between: Heavy-weight processes: supposedly completely separate from one another. Light-weight processes (threads): which share a memory space (as well as other resources) in common. DEFINITION 1. A thread is a string of code capable of executing alongside other processes. Threads do not execute at the same time but rather using shared time, this is why it is important that a thread always gives others a chance to execute. The diagram below shows the execution and latency times for four different threads. Figure 1.2. Execution and latency times of four different threads 1.2. Concurrent computing A concurrent programming language must allow the following: creation of threads; sharing data between threads; synchronization among threads: controlling the task execution order depending on the two following models:

Introduction to Threads in Java 3 - Competition synchronization: when more than one thread is using the same resource. There must then be a system for mutual exclusion in order to avoid processes interfering with each other. - Cooperation synchronization: when one thread waits for another to finish executing before starting its own execution. 1.3. Thread creation There are two ways to create a thread in Java: A class derived from java.lang.thread: - The java.lang.thread implements Runnable class. - Thread extends Object implements Runnable public class. - The Thread class must implement the run() method. - The daughter class inherits the run() method. A class that implements the Runnable interface: - The class must implement the run() method. Now, a question arises: which solution should we choose? Method 1: subclassing Thread: - When parallelizing a class which does not inherit from another class (autonomous class). - Note: simple inheritance in java. - extends Thread or implements Runnable, either way. Method 2: implement Runnable: - When a superclass is imposed. - Example, case involving applets: public class MyThreadApplet extends Applet implements Runnable { - Only implements Runnable is valid.

4 Concurrent, Real-Time and Distributed Programming in Java Let us look at the code: Method 1: subclassing Thread class A extends Thread { A ( ) {... // The constructor... public void run ( ) {... // What the Thread does A p1 = new A( ); // Creation of thread p1 p1.start(); // Starts the thread and executes p1.run() Method 2: class that implements Runnable class B implements Runnable { B ( ) {... // Constructor... public void run() {... // What the Thread does B p = new B( ); Thread p2 = new Thread(p);... p2.start(); // Starts the thread and executes p.run() 1.4. Types of thread Two types of thread exist: User threads: this type of thread s activity is time-restricted, meaning its scenario is a process which ends after a certain amount of time. The JVM functions as long as there are user threads being executed. Daemon threads: threads that execute in the background as long as the program is running. Daemon threads are only there to serve user threads. The JVM stops if there are only daemons.

Introduction to Threads in Java 5 Examples: syntax coloring in editors, garbage collectors (DestroyJavaVM), etc. public final boolean isdaemon() in the Thread class to determine the thread s type. public final void setdaemon (boolean) of the Thread class indicates whether the thread will be a daemon or not (user thread by default). It must be called before the thread starts using the start() command. Use setdaemon in the constructor. Example: clock thread running in the background. Clock class extends Thread { public clock () { setdaemon (true); public void run () { while (true) { try {Thread.sleep (300) ; catch (InterruptedException e) { System.out.println ("tip"); // end while // end run public static void main(string arg[]){ A p1 = new A( ); p1.start(); // end main // end class 1.5. Monotask versus multitask To illustrate multitask, let us begin by presenting a monotask execution. In the following example, a parrot (Parrot0.java) converses in monotask with the primary program (ChatAndLaunchTheParrot0.java): class ChatAndLaunchTheParrot0 { public static void main(string args[]) { Parrot0 parrot = new Parrot0 ("coco",4);

6 Concurrent, Real-Time and Distributed Programming in Java blabla(); blabla(); parrot.run (); for (int n=0; n<3; n++) { try {Thread.sleep(1000); catch (InterruptedException e) { System.out.println (e.getmessage ()); System.exit(1); blabla (); // end main private static void blabla() { System.out.println("blabla"); // end class class Parrot0 { private String cri = null; private int fois = 0; public Parrot0 (String s, int i) { cri = s; fois = i; public void run () { for (int n=0; n<fois; n++) { try {Thread.sleep (1000); catch (InterruptedException e) { System.out.println(e.getMessage()); System.exit(1); System.out.println (cri); // end for // end run // end class Executing the previous code results in the following: blabla blabla

Introduction to Threads in Java 7 coco coco coco coco blabla blabla blabla Nothing special, the primary program talks, passes to the parrot s run and the execution ends with the three blabla of the primary program. To perform the multitask, a modification of the previous code is necessary. We will perform this in two different ways. Extending the Thread class: class ChatAndLaunchTheParrot2{ public static void main(string args[]) { Parrot2 parrot = new Parrot2 ("coco",10); parrot.start(); for (int n=0; n<10; n++) { try { Thread.sleep(1000); catch(interruptedexception e) { blabla(); private static void blabla() { System.out.println("blabla"); class Parrot2 extends Thread{ private String cri = null; private int fois = 0;