timer - Multi-Threading Java supports the creation of...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Multi-Threading Java supports the creation of programs with concurrent flows of control. Threads -- independent flows of control. (They are light weight processes.) We first look at two Timer classes to have concurrent flows. There are two Timer classes. One is in java.util package and another in javax.swing package. They have a different set of methods, thus slightly different functionality. Multiple threads on multiple CPUs Multiple threads sharing a single CPU
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Timer class in java.util uses TimerTask class. TimerTask is an abstract class with the abstract method run(). ( public void run()) The action to be performed by this timer task needs to be defined in the run() method. Timer class (java.util) is thread-safe. Multiple thread can share a single Timer object without the need for external synchronization. Timer class (defined in java.util package) We can use Timer class to allow a thread to be created and run either at a time relative to the current time or at some specific time. An object of Timer class can be created as: Timer timer = new Timer(); It has a schedule() method (with method overloading) for creating thread after either some specified delay or at some specific time. Running after a delay public void schedule(TimerTask task, long m) runs task.run() after waiting m millisecond. Running Repeatedly public void schedule(TimerTask task, long m, long n) runs task.run() after waiting m millisecond. It then repeatedly re-runs task.run() every n milliseconds. Running at a chosen time public void schedule(TimerTask task, Data t) runs task.run() at time(date) t. public void cancel() cancel the timer task.
Background image of page 2
(using sequential process): The following program generates sequential processing. It means that it executes only one method at time. Each constructor method prints a character 20 times. public class DisplayChar { public static void main(String[] args) { DisplayCharSequenceSeq s1 = new DisplayCharSequenceSeq('A'); DisplayCharSequenceSeq s2 = new DisplayCharSequenceSeq('B'); DisplayCharSequenceSeq s3 = new DisplayCharSequenceSeq('C'); } } public class DisplayCharSequenceSeq { private char displayChar; // Constructor
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

timer - Multi-Threading Java supports the creation of...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online