[Practice] Java callback Functions

Source: Internet
Author: User
The following uses the Java callback function to implement a tool class for testing the function runtime:

If we want to test the execution time of a class method, we usually do this:

Java code
  1. Public class testobject {
  2. /**
  3. * A method used for testing carries out a time-consuming cycle.
  4. */
  5. Public static void testmethod (){
  6. For (INT I = 0; I <100000000; I ++ ){
  7. }
  8. }
  9. /**
  10. * A Simple Method for Testing the execution time
  11. */
  12. Public void testTime (){
  13. Long begin = System. currentTimeMillis (); // Test start time
  14. TestMethod (); // Test Method
  15. Long end = System. currentTimeMillis (); // test end Time
  16. System. out. println ("[use time]:" + (end-begin); // print the usage time
  17. }
  18. Public static void main (String [] args ){
  19. TestObject test = new TestObject ();
  20. Test. testTime ();
  21. }
  22. }

As you can see, only the "// test method" of the testtime () method needs to be changed. Let's create a function to implement the same function but be more flexible:

First, define a callback interface:

Java code
  1. Public interface CallBack {
  2. // Method for executing the callback operation
  3. Void execute ();
  4. }

Then write a tool class:

Java code
  1. Public class Tools {
  2. /**
  3. * Test the function usage time by defining the execute method of the CallBack Interface
  4. * @ Param callBack
  5. */
  6. Public void testTime (CallBack callBack ){
  7. Long begin = System. currentTimeMillis (); // Test start time
  8. CallBack.exe cute (); // call back
  9. Long end = System. currentTimeMillis (); // test end Time
  10. System. Out. println ("[use time]:" + (end-begin); // print the usage time
  11. }
  12. Public static void main (string [] ARGs ){
  13. Tools tool = new tools ();
  14. Tool. testtime (New callback (){
  15. // Define the execute Method
  16. Public void execute (){
  17. // Add one or more methods to test the running time.
  18. TestObject. testMethod ();
  19. }
  20. });
  21. }
  22. }

As you can see, the callback function can be implemented by passing testtime () to the execute () method that defines the callback interface.

 

Go to: If you forget it, use it if you think it is useful. Thank you.

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.