T.0. Tasks - JulTob/Ada GitHub Wiki

This writes the abc... twice. Once small caps, then big caps.

with Ada.Text_IO; use Ada.Text_IO;

procedure hello is -- implicitly called by the environment task
  task My_Task;
  task body My_Task is
    begin
    for I in character range 'A'..'Z' loop
      Put_Line (" " & I);
      end loop;
    end My_Task;
  begin
  for I in  character range 'a' .. 'z' loop
    Put_Line (" " & I);
    end loop;
  end hello;

Any number of Ada tasks may be declared in any declarative region.

A task declaration is very similar to a procedure or package declaration.

They all start automatically when control reaches the begin.

A block will not exit until all sequences of statements defined within that scope, including those in tasks, have been completed.

A task type is a generalization of a task object; each object of a task type has the same behavior.

A declared object of a task type is started within the scope where it is declared, and control does not leave that scope until the task has terminated.

An Ada task type is somewhat analogous to a Java Thread subclass, but in Java the instances of such a subclass are always dynamically allocated.

In Ada an instance of a task type may either be declared or dynamically allocated.

Task types can be parametrized; the parameter serves the same purpose as an argument to a constructor in Java.

The following example creates 10 tasks, each of which displays a subset of the alphabet contained between the parameter and the ‘Z’ Character.

As with the earlier example, since there is no synchronization among the tasks, the output may be interspersed depending on the implementation’s task scheduling algorithm.

task type My_Task (First : Character);
task body My_Task (First : Character) is
   begin
   for I in First .. ’Z’ loop
      Put_Line (I);
      end loop;
   end My_Task;

procedure Main is
   Tab : array (0 .. 9) of My_Task (’G’);
   begin
   null;
   end Main;

In Ada a task may be allocated on the heap as opposed to the stack.

The task will then start as soon as it has been allocated, and terminates when its work is completed.

This model is probably the one that’s the most similar to Java:

type Ptr_Task is access My_Task;

procedure Main is
   T : Ptr_Task;
  begin
   T := new My_Task ('G');
  end Main;