"Getting Started with Java" Day5 callbacks in Java (ii)

Source: Internet
Author: User

There are many timer in Java, there are two timer classes, a timer under the Java.util package, a timer under the Javax.swing package, and two timer classes are useful to the callback mechanism. You can use it to send notifications after the specified time interval, such as having a clock in the program, and you can request a notification every second to update the time display. So how do we tell the timer to do our job? In other languages, you can provide a function name, and then the timer calls it periodically, but Java uses the idea of object-oriented programming, passing the object of a class to the timer, and then invoking the method of the object, because the object can carry more information than the function, so it will be more flexible.

Just to illustrate the callback, just describe the method of the timer that needs to be used, not a detailed introduction to the timer.

Taking the timer in swing as an example, the object that the timer receives must first implement the ActionListener interface in Java.awt.event, and the following is the prototype of the interface:

 Public Interface actionlistener{    void  actionperformed (ActionEvent event);}

So we first design a class to implement this interface, here we override this actionperformed method to output time.

 Public class Implements ActionListener {    @Override    publicvoid  actionperformed (ActionEvent e) {         New  Date ();        System.out.println (' time is ' + Now + ' now. ' );    }}

Then we can generate a Timerprinter object to pass in the timer.

 Public class Test {    publicstaticvoid  main (string[] args) {        new Timeprinter ();         New Timer (listener);        T.start ();        Joptionpane.showmessagedialog (null, "Quit program?") )///Popup dialog to prevent the end of the main thread from causing the clock to end        System.exit (0);}    }

Here we use a popup dialog statement, do not need to drill down, as long as it is known to block the main thread, so that the clock can continue to run OK. The following is the result of the operation:

Time is Mon Dec 00:01:20 CST00:01:21 CST00:01:22 CST00:01:23 CST 201 700:01:24 CST00:01:25 CST now.

Output once per second, that is, the actionperformed method of Timeprinter is called once per second, which is the most typical callback (it is a bit complicated to feel the chestnuts on the previous section).

In fact, you can understand the callback, that is, I give a method of processing power to you, you can use this method at the right time, then how do you know which method to use? This time to play the role of the interface, the implementation of the interface will represent the existence of this function, so you can be assured of the use of the bold.

"Getting Started with Java" Day5 callbacks in Java (ii)

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.