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

Concurrent, Real-Time and Distributed Programming in Java

Threads, RTSJ and RMI

Badr Benmammar

  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

Badr Benmammar

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
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.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Concurrent, Real-Time and Distributed Programming in Java an online PDF/ePUB?
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 Informatik & Programmierung in Java. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Wiley-ISTE
Year
2017
ISBN
9781119482758

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

Citation styles for Concurrent, Real-Time and Distributed Programming in Java

APA 6 Citation

Benmammar, B. (2017). Concurrent, Real-Time and Distributed Programming in Java (1st ed.). Wiley. Retrieved from https://www.perlego.com/book/993401/concurrent-realtime-and-distributed-programming-in-java-threads-rtsj-and-rmi-pdf (Original work published 2017)

Chicago Citation

Benmammar, Badr. (2017) 2017. Concurrent, Real-Time and Distributed Programming in Java. 1st ed. Wiley. https://www.perlego.com/book/993401/concurrent-realtime-and-distributed-programming-in-java-threads-rtsj-and-rmi-pdf.

Harvard Citation

Benmammar, B. (2017) Concurrent, Real-Time and Distributed Programming in Java. 1st edn. Wiley. Available at: https://www.perlego.com/book/993401/concurrent-realtime-and-distributed-programming-in-java-threads-rtsj-and-rmi-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Benmammar, Badr. Concurrent, Real-Time and Distributed Programming in Java. 1st ed. Wiley, 2017. Web. 14 Oct. 2022.