Wednesday 2 March 2016

The Runnable Interface

We’ve implemented the Runnable interface in HelloComponent4. To create a thread, the HelloComponent4 object passes itself (this) to the Thread constructor. This means that HelloComponent4 must implement the Runnable interface by implementing the run() method. This method is called automatically when the runtime system needs to start the thread. We indicate that the class implements the interface in our class declaration:

 public class HelloComponent4  
 extends JComponent  
 implements MouseMotionListener, ActionListener, Runnable {...}  
At compile time, the Java compiler checks to make sure we abide by this statement. We have carried through by adding an appropriate run() method to HelloComponent4. It takes no arguments and returns no value. Our run() method accomplishes blinking by changing the color of our text a few times a second. It’s a very short routine, but we’re going to delay looking at it until we tie up some loose ends in dealing with the Thread itself.

0 comments:

Post a Comment