Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real time thread synchronization with normal threads

How would one synchronize one real-time thread with a normal thread in Java? Take for example a real-time thread that calculates some temperatures and the GUI thread that has to collect those numbers and show them on some controls.

The implementation of the real-time library shouldn't matter as it should follow the specifications.

like image 370
hyperboreean Avatar asked Feb 14 '09 00:02

hyperboreean


People also ask

Can two threads run at the same time?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

What is a real time thread?

Real-time threads are schedulable objects and, therefore, can have associated release, scheduling, memory and processing group parameters. A real-time thread can also have its memory area set.

How does one ensure thread Synchronisation such that all threads wait until all threads arrive?

Barriers. Barriers are a synchronization mechanism that can be used to coordinate multiple threads working in parallel. A barrier allows each thread to wait until all cooperating threads have reached the same point, and then continue executing from there.

How do you synchronize threads?

If you declare any method as synchronized, it is known as synchronized method. Synchronized method is used to lock an object for any shared resource. When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.


3 Answers

You're going to need two things. You want your real-time thread to get priority, and preferably have it driven by a timer so you get (nearly) periodic measurements. Then, you're going to want a resource that can be used to communicate the values. That can either be a simple monitor, with a critical section so the timer thread can write into it, or it could be -- and very probably would be better as -- a FIFO, so that the GUI thread can eat up values whenever it has cycles.

In Java, the GUI (at least in Swing and similar) is already running a separate thread for UI interactions, so you're big issue is to set up your measurement thread. Have a look at Runnables.

like image 112
Charlie Martin Avatar answered Oct 03 '22 09:10

Charlie Martin


To use real-time threads you need Real Time Java on real time operating system. http://java.sun.com/javase/technologies/realtime/index.jsp

However if you have a thread which is latency sensitive, I suggest you;

  • use the concurrency libraries in communications with other threads.
  • minimise any GC activity (esp full GCs)
  • don't run the thread in the same process as a GUI if you can (as it tends grab a lot of resources in ways you have limited control over)
like image 27
Peter Lawrey Avatar answered Oct 03 '22 08:10

Peter Lawrey


Since others have brought up RTSJ, I'll comment that synchronization between real-time and non-real-time code has a number of solutions. RTSJ provides wait-free queues for such communication. It is also possible to build upon these or other queues and make use of RTSJ's AsyncEvent and AsyncEventHandler abstractions to manage the communication. This is appropriate for situations where you really, truly have a need for deterministic behavior for the "real-time" thread.

If you can accept best-effort behavior (try really hard to hit your deadlines, but the world doesn't fall apart if you miss) I suggest building carefully on the executor framework provided by the Java concurrency utilities. A careful selection of task boundaries, a suitable queuing policy (here, "suitable" would depend on more detail about your application than you've given), and threadpool policy will get what you need.

like image 29
andersoj Avatar answered Oct 03 '22 09:10

andersoj