Concurrent, Real-Time and Distributed Programming in Java
eBook - ePub

Concurrent, Real-Time and Distributed Programming in Java

Threads, RTSJ and RMI

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Concurrent, Real-Time and Distributed Programming in Java

Threads, RTSJ and RMI

About this book

This book provides an introduction to concurrent, real-time, distributed programming with Java object-oriented language support as an algorithm description tool. It describes in particular the mechanisms of synchronization (cooperative and competitive) and sharing of data (internal class, static variables) between threads in Java. He then discusses the use of Java for real-time applications. Consequently, a presentation of the RTSJ (Real Time Specification for Java) specification dedicated to the development of real-time applications in Java is also introduced in this book. Finally, a presentation of programming distributed in Java is presented in this book. We are particularly interested in communication using the TCP Sockets and high-level communication using Java Remote Method Invocation (RMI). The book also contains an annex which contains a practical set of application exercises in relation to the theme of the book. Knowledge of the Java language is a prerequisite for understanding the book.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Concurrent, Real-Time and Distributed Programming in Java by Badr Benmammar in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

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.
image
Figure 1.1. Isolation between processes
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.
image
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:
    • - 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.
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.
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 ru...

Table of contents

  1. Cover
  2. Table of Contents
  3. Title
  4. Copyright
  5. List of Acronyms
  6. Introduction
  7. 1 Introduction to Threads in Java
  8. 2 Thread Synchronization
  9. 3 Real-Time Systems and Real-Time Java
  10. 4 Distributed Programming in Java
  11. Appendix
  12. Bibliography
  13. Index
  14. End User License Agreement