Beginner Multithreading Practice Electronic Clock

Source: Internet
Author: User

simulates an electronic clock that can be started or stopped at any time, and can run independently.

1. Define a clock class. It inherits the label class and implements the Runnable interface. This class has a thread-type Clocker domain, and the start () and run () methods. In the Run () method, the system time is displayed as the text of the label every second.

1 classClockextendsLabelImplementsRunnable2 {3     //define the Clocker domain for the thread type4      PublicThread clocker=NULL;5      PublicClock ()6     {7         8         //when initializing, sets the label to the current system time9         //call the ToString method to convert to string typeTenSetText (NewDate (). toString ()); One     } A     //controlling the startup of threads -      Public voidStart () -     { the         if(clocker==NULL) -         { -             //Clocker is initialized by the object that is obtained by the thread class construction method, and the current object of the clock class as a parameter -Clocker=NewThread ( This); + Clocker.start (); -         } +  A     } at     //controlling the stop of a thread -      Public voidStop () -     { -Clocker=NULL; -     } -     //implementing the Run () method in the Runnable interface in      Public voidRun () -     { toThread currentthread=Thread.CurrentThread (); +         //determine if Clocker is the currently running thread -          while(clocker==CurrentThread) the         { *SetText (NewDate (). toString ()); $                        TryPanax Notoginseng{//sleep 1s clock -Clocker.sleep (1000); the             } +             Catch(interruptedexception IE) A             { theSYSTEM.OUT.PRINTLN ("Thread Error:" +IE); +             } -         } $      $     } -  -}

2. Define a Clockframe class. It inherits the frame class and implements the ActionListener interface. Define the start and stop buttons in this class to control the operation of the electronic clock. And this class has a clock class domain, the clock class object is added to the Clockframe class display.

1  Public classClockframeextendsFrameImplementsActionListener2 {3     PrivateButton start=NewButton ("Start");4     PrivateButton stop=NewButton ("Stop");5     PrivateClock c=NewClock ();6      PublicClockframe ()7     {8         Super("Electronic Clock");9         //set up a form to use a streaming layoutTenSetLayout (NewFlowLayout ()); One         //Add a button and register a listener for it A Add (start); -Start.addactionlistener ( This); - Add (stop); theStop.addactionlistener ( This); -         //using an anonymous inner class that inherits Windowadapter to close the window -Addwindowlistener (NewWindowadapter () -         { +              Public voidwindowclosing (windowevent We) -{system.exit (0);} +         }); A Add (c); at         //make the component in the window to get reasonable arrangement.  - pack (); -SetVisible (true); -  -  -     } in     //The response to an event is implemented by invoking a method in the clock object.  -      Public voidactionperformed (ActionEvent ae) to     { +         if(Ae.getsource () = =start) -         { the C.start (); *         } $         ElsePanax Notoginseng             if(Ae.getsource () = =stop) - c.stop (); the  +     } A      Public Static voidMain (string[] args) the     { +         Newclockframe (); -     } $}

3. Operation:

Note:

Classes that need to be imported:

import java.awt.*; import java.awt.event.*; import java.util.Date;

Beginner Multithreading Practice Electronic Clock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.