Ada Tasks and Java Threads

These are lecture notes from my Computer Science course. For learning about real-time systems, I recommend Real-Time Systems and Programming Languages.

Ada task specifications explain how you can communicate with them. The body can be compiled separately from the specification (normal Ada stuff). You can have tasks that have no way of communicating with them (a one-way client type situation). There are some examples on slides.

task type Garage_Attendant(
		Pump : Pump_Number := 1) is
	entry Serve_Leaded(G : Gallons);
	entry Serve_Unleaded(G : Gallons);
end Garage_Attendant;

Note the ability to set default values using the := 1 bit.

Because tasks are just types, you can store them in arrays or records or whatever. A bit like how in Erlang you have a PID atom like <#0.392> which is really just an atom of a certain format.

Typically a task will use a loop structure.

You can use ‘new T’ to create dynamic tasks. You can override pointers to tasks, in which case the old task is just floating around.

What does an Ada task do?

  • Activation: sort out local variables etc.
  • Normal Execution: standard stuff, body of the task.
  • Finalisation: any finalisation code.

There’s a slide in Lecture 6 that has a big graph of what happens to a task.

Ada has task hierarchy. The master of a dependent task must wait for it to terminate before it can terminate.

You can detect if a task has terminated with code like;

if T'Terminated then	-- for some task T
	-- error recovery action
end if;

But it cannot tell the difference between ordinary termination or error termination. Also watch out for race conditions (of sorts).

COBEGIN and PAR (used in the programming language occam) can be represented in Ada if you really want it to. Just put all the tasks in an encapsulating task, and that one has to wait until the other 3 have terminated.

Java Threads

Ada uses language syntax to do concurrency. Java uses java.lang.Thread to provide the mechanisms to create threads.

There’s a standard interface Runnable that can be used so that you don’t have to make child classes of Thread all the time. Your class just needs to implement a run method.

Java has two types of thread; user and daemon. You can probably guess what they do. You call the setDaemon method before the thread is started to make it into a daemon. Setting a thread to a daemon thread basically says ‘you can kill me if you want to terminate; don’t wait for me’.

When creating Java threads; either extend from the Thread class and override the run method and start it the run method will execute. Or implement the Runnable interface by providing a run method, and then you pass in the Runnable objects into new Thread(). The second method is more flexible I believe.

Concurrency. Language or OS?

Language-based concurrency is an alternative to OS-based concurrency. Ada and Java both use language-based concurrency. Quite a few advantages to that. Erlang uses it too, but then it runs in a virtual machine like Java.

This entry was posted in lecture, rts. Bookmark the permalink.