Concurrent, Real-Time and Distributed Programming in Java
Threads, RTSJ and RMI
Badr Benmammar
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Concurrent, Real-Time and Distributed Programming in Java
Threads, RTSJ and RMI
Badr Benmammar
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
Information
1
Introduction to Threads in Java
1.1. Processes versus threads
- – 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.
1.2. Concurrent computing
- – 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
- – 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?
- - The class must implement the run() method.
- – 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.
- – 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
- – 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.
- – 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.