Friday 26 February 2016

Thread vs Timers

One of the most basic uses of threads is to perform some periodic task at set intervals. In fact, this is so basic that there is a specialized class for performing this task—and you’ve already worked with it. The Timer class, in package javax.swing, can generate a sequence of ActionEvents separated by a specified time interval. Behind the scenes, a Timer uses a thread. The thread sleeps most of the time, but it wakes up periodically to generate the events associated with the timer. Before timers were introduced, threads had to be used directly to implement a similar functionality. In a typical use of a timer for animation, each event from the timer causes a new frame of the animation to be computed and displayed. In the response to the event, it is only necessary to update some state variables and to repaint the display to reflect the changes. A Timer to do that every fifty milliseconds might be created like this:

Timer timer = new Timer( 30, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
updateForNextFrame();
display.repaint();
}
} );
timer.start();

Suppose that we wanted to do the same thing with a thread. The run() method of the thread would have to execute a loop in which the thread sleeps for 50 milliseconds, then wakes up to do the updating and repainting. This could be implemented in a nested class as follows using the method Thread.sleep().

 private class Amar extends Thread {
 public void run() {
while (true) {
try {
Thread.sleep(50);
}
catch (InterruptedException e) { 
}
updateForNextFrame();
display.repaint();
                                                                                }
                                                                           }
                                                                    }

To run the animation, you would create an object belonging to this class and call its start() method. As it stands, there would be no way to stop the animation once it is started. One way to make it possible to stop the animation would be to end the loop when a volatile boolean variable, terminate, becomes true.
              There is a subtle difference between using threads and using timers for animation. The thread that is used by a Swing Timer does nothing but generate events. The event-handling code that is executed in response to those events is actually executed in the Swing event-handling GUI thread, which also handles repainting of components and responses to user actions. This is important because the Swing GUI is not thread-safe. That is, it does not use synchronization to avoid race conditions among threads trying to access GUI components and their state variables. As long as everything is done in the Swing event thread, there is no problem. A problem can arise when another thread manipulates components or the variables that are also used in the GUI thread. In the Animator example given above, this could happen when the thread calls the updateForNextFrame() method. The variables that are modified in updateForNextFrame() would also be used by the paintComponent() method that draws the frame. There is a race condition here: If these two methods are being executed simultaneously, there is a possibility that paintComponent() will use inconsistent variable values—some appropriate for the new frame, some for the previous frame.
             One solution to this problem would be to declare both paintComponent() and
updateForNextFrame() to be synchronized methods. A better solution in this case is to
use a timer rather than a thread.
This is a simple article which showed you that how thread and timers differs in their processing. 
Thank you guys.