Documentos de Académico
Documentos de Profesional
Documentos de Cultura
4.10.1Timing problems
4.10.2 Classes and solutions for timing
4.10.3 Priorities of timed threads
Constructor Summary
Timer()
Creates a new timer.
Timer(boolean isDaemon)
Creates a new timer whose associated thread may be specified to run as a
daemon.
Method Summary
Tiberiu Leia : Real-Time Systems Timing Programming Java Programming 5
void cancel()
Terminates this timer, discarding any currently scheduled tasks.
void schedule(TimerTask task, Date time)
Schedules the specified task for execution at the specified time.
void schedule(TimerTask task, Date firstTime,
long period)
Schedules the specified task for repeated fixed-delay execution,
beginning at the specified time.
void schedule(TimerTask task, long delay)
Schedules the specified task for execution after the specified
delay.
void schedule(TimerTask task, long delay,
long period)
Schedules the specified task for repeated fixed-delay execution,
beginning after the specified delay.
void scheduleAtFixedRate(TimerTask task,
Date firstTime, long period)
Tiberiu Leia : Real-Time Systems Timing Programming Java Programming 6
Schedules the specified task for repeated fixed-rate execution,
beginning at the specified time.
void scheduleAtFixedRate(TimerTask task, long delay,
long period)
Schedules the specified task for repeated fixed-rate execution,
beginning after the specified delay.
This class does not offer real-time guaranties. It schedules tasks using the
Object.wait(long) method.
public void schedule(TimerTask task, long delay, long period)
Method Summary
boolean cancel()
Cancels this timer task.
abstract void run()
The action to be performed by this timer task.
long scheduledExecutionTime()
Returns the scheduled execution time of the most
recent actual execution of this task.
MainClass Timer
+main() +schedule()
TimerTask PeriodicTask
+period: long
+run()
+scheduleExecutionTime() +run()
+scheduleExecutionTime()
import java.util.Timer;
public class MainClass {
public static void main(String[] args) {
PeriodicTask pt1=new PeriodicTask(1000,"pt1");
PeriodicTask pt2=new PeriodicTask(2000,"pt2");
Timer tt=new Timer();
tt.schedule(pt1,1000,pt1.period);
tt.schedule(pt2,1000,pt2.period); Homework: build the TPN
} model and analyze the
} application behavior.
Methods:
boolean add(E o) E Element
void clear()
Iterator iterator()
boolean offer(E o) Insert the specific element into the queue (right position)
void put(E o) Adds the E to queue
boolean offer(E o, long timeout, TimeUnit unit)
E take() retrieve and removes
DelayQueue
Producer Consumer
+add()
+depositTime: String +put() +retrieveTime: String
+sentMessage: String +offer() +receivedMessage: String
+delayTime: String +take()
+poll() +run()
+run() +peek()
+remove()
<<Interface>>
Runnable
TimerTask
MainClass +run()
+prod: Producer
+cons: Consumer
+run() +run()
+start()
Timer
+schedule()
display massage
wait children
wait wait(1 milisec)
create element
<<DelayQueue>>
decrement
<<Dimension n>>
[DelayQueue]
display element
continue
take element
wait 800 ms
display element
java.util.concurrent.ScheduledThreadPoolExecutor
A ThreadPoolExecutor that can additionally schedule commands to run after a given
delay, or to execute periodically. This class is preferable to Timer when multiple
worker threads are needed, or when the additional flexibility or capabilities of
ThreadPoolExecutor (which this class extends) are required.
Delayed tasks execute no sooner than they are enabled, but without any real-time
guarantees about when, after they are enabled, they will commence. Tasks scheduled
for exactly the same execution time are enabled in first-in-first-out (FIFO) order of
submission.
While this class inherits from ThreadPoolExecutor, a few of the inherited tuning
methods are not useful for it. In particular, because it acts as a fixed-sized pool using
corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have
no useful effect.
Tiberiu Leia : Real-Time Systems Timing Programming Java Programming 25
Extension notes: This class overrides AbstractExecutorService submit methods to
generate internal objects to control per-task delays and scheduling. To preserve
functionality, any further overrides of these methods in subclasses must invoke
superclass versions, which effectively disables additional task customization.
However, this class provides alternative protected extension method decorateTask (one
version each for Runnable and Callable) that can be used to customize the concrete
task types used to execute commands entered via execute, submit, schedule,
scheduleAtFixedRate, and scheduleWithFixedDelay. By default, a
ScheduledThreadPoolExecutor uses a task type extending FutureTask.
It may be modified or replaced using subclasses of the form:
public class CustomScheduledExecutor extends
ScheduledThreadPoolExecutor {
static class CustomTask<V> implements
RunnableScheduledFuture<V> { ... }
protected <V> RunnableScheduledFuture<V> decorateTask(
Runnable r, RunnableScheduledFuture<V> task) {
return new CustomTask<V>(r, task);
}
+run() +myCall()
MyScheduled
MainClass
+main()
Server:
secvenial - sequential
multifir - multithreading
Arhitectures:
Filter Pipeline = Conducte pentru filtrare
Supervisor-Worker = Supervizor-muncitor
Announcer-Listener = Crainic-asculttor
Planned tasks execution
Image processing
Supervisor Bag
O soluie posibil:
<<ActiveObject>>
supervisor offered() release()
+put()
<<LinkedBlockedQueue>> <<ActiveObject>>
take() answerQueue
tryAquire() +put() worker2
execute()
release()
<<Semaphore>>
sem
sd Activity[]
act[0]
execute()
act[1]
+1
1 .. n
Element <<Interface>> Executor
Delayed
+compareTo() +execute()
+compareTo()
+getDelay()
Semaphore
+release()
+1 +tryAcquire()
The message contains the requested activity and the time (delay) to perform it.
What can be done if the application doesnt fulfill the timing requirements?
Can be known before starting (a priory) if the application meets the deadlines?