|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object jcontrol.lang.Deadline
public class Deadline
JControl deadline.
In embedded control there is often the need for realtime tasks. Some tasks are perodically repeating and some are sporadical but in both cases the calculation has to be finished in a specified period of time (time constraint).
The JControl virtual machine is beside the normal priority driven multithreading scheme able to assign deadlines to sequences of code. Then the scheduler is switched to EDF (earliest deadline first) mode. So a thread is privileged as long it has the most pressing deadline compared to other threads (and threads without deadline).
JControl deadlines depend on thread synchronisation. To enter a deadline you have to
enter a monitor (a synchronized
block) on the deadline object. So the
same mechanism used for thread synchronization is used for deadline consistency.
For the same reason you can't use the same deadline object on different threads
at the same time.
Realtime tasks have different demands, all these demands are covered with the JControl deadline concept, some examples explain these.
the deadline is entered in the moment of entering the monitor, not at object creation time. At the moment of leaving the monitor, the deadline is checked and a wait is forced for the remaining time. If your code took too much time to process, your application is able to check this at runtime:synchronized(new Deadline(25)) { [...] // your code that has to be processed in 25ms }
TheDeadline d=new Deadline(25); synchronized(d) { [...] // your code that has to be processed in 25ms try{ d.check(); } catch(DeadlineMissException e) { [...] // emergency code sequence } }
check()
command behaves like Object.wait()
as a
IllegalMonitorStateException
is thrown if your thread is not the owner of the
monitor of the deadline object (if not invoked from within a synchronized block).
You can also put the try
-catch
outside the synchronized
block in this case the catch
sequence is processed in normal priority scheduling
mode.
You can recycle the deadline object after using in this or in another thread, in both
cases the deadline is set to the new point of time at the moment entering the monitor.
Another way of entering a deadline is overriding the Deadline
class and
create some synchronized methods, e.g. an anonymous class:
new Deadline(25) { public synchronized void critical(){ [...] // your code that has to be processed in 25ms } }.critical();
SoDeadline d=new Deadline(25); try{ synchronized(d) { for(;;) { [...] // your code that has to be processed in 25ms d.append(25); } } } catch(DeadlineMissException e) { [...] // emergency code sequence }
append()
behaves like check()
and leaving and entering the
synchronized block combined: the DeadlineMissException
could be thrown if the
deadline was missed or the current thread is forced waiting until the old deadline is reached.
But there is one difference: the new deadline is immediately appended to the old one, so no
time is lost (which would happen if you first leave the EDF scheduled block and then enter
it again).
It's not required to append equidistant deadlines, it is also possible to determine the times at runtime:
In this example the deadlines are varying from 1 to 25, note that the fist time constraint is defined at theDeadline d=new Deadline(1); synchronized(d) { for(int x=2;x<=25;x++) { [...] // your code that has to be processed in x-1 ms try{ d.append(x); catch(DeadlineMissException e) { [...] // emergency code sequence } } }
Deadline
constructor and all others by the append
command. Here the catch
is inside the loop so if a deadline is missed (more
likely in the beginning) the emergency code sequence is processed but the new deadline is
still appended to the (already missed) old one. So even if the one or the other deadline
was missed it is possible to catch up the sequence with longer constraints later.
wait
-notify
scheme also works with deadlines. If you do a
wait()
on the deadline object, the monitor and the deadline are left. If the
thread continues execution triggered by a notify
or another thread a new
deadline is entered immediately:
Deadline d=new Deadline(25); try{ synchronized(d) { for(;;) { d.wait(); // tempoary leaving the deadline (and EDF privileges) [...] // your code that has to be processed in 25ms } } } catch(DeadlineMissException e) { [...] // emergency code sequence }
synchronized
within a synchronized
has no effect (no monitor
is entered). It is possible to synchronize to another deadline object then the
current deadline is memorized and overwritten by the new one (it doesn't make any difference
if the new timeconstraint will result in a more or less pressing deadline than the current).
After leaving the inner synchronized block the old deadline is restored. This works also
if the inner synchronized block is within another class, e.g. if an API method uses deadlines
for its implementation, this doesn't affect the application behaviour (assumed that the
inner deadline is earlier than the outer).
synchronized(new Deadline(25)) { synchronized(new Deadline(10)){ [...] // your code that has to be processed in 10ms } [...] // your code that has to be processed in 25ms // (combined with the 10 above) }
Constructor Summary | |
---|---|
Deadline(int timeconstraint)
Constructs a new JControl deadline. |
Method Summary | |
---|---|
void |
append(int newTimeconstraint)
Appends a new timeconstraint to the currently valid deadline. |
void |
check()
Just checks if the current deadline has run out. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, notifyAll, toString, wait |
Constructor Detail |
---|
public Deadline(int timeconstraint)
useTimeconstraint
- the timeconstraint in milliseconds to generate
the deadline from at the time of synchronizationMethod Detail |
---|
public void append(int newTimeconstraint) throws IllegalMonitorStateException, DeadlineMissException
newTimeconstraint
- the timeconstraint to generate the new deadline from the end of the old deadline
IllegalMonitorStateException
- if the current thread isn't locked to this deadline
DeadlineMissException
- of the old deadline has run outpublic void check() throws IllegalMonitorStateException, DeadlineMissException
synchronized
block.
IllegalMonitorStateException
- if the current thread isn't locked to this deadline
DeadlineMissException
- of the old deadline has run out
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |