android-java synchronous method and asynchronous method

interface

A Java interface is a declaration of a series of methods. It is a collection of method characteristics. An interface has only method characteristics and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors ( Function).
Two meanings: first, the Java interface, the structure existing in the Java language, has a specific syntax and structure; second, the feature set of a class's methods is a logical abstraction. The former is called "Java interface" and the latter is called "interface". In java, the interface is represented by the keyword interface, and the writing method is:

public interface Listener{
    void result();
}

And the name of the interface that follows is XX_Listener according to the custom, that is, the meaning of the listener.

In the Java language specification, the characteristics of a method include only the name of the method, the number and types of parameters, but not the return type, parameter names, and exceptions thrown by the method. When the Java compiler checks the overloading of the method, it will judge whether the two methods are overloaded methods according to these conditions. But when the Java compiler checks the replacement of the method, it will further check whether the return type and the thrown exception of the two methods (sub-type and sub-type) are the same.
The rules of interface implementation and class inheritance are different. For data security, a class has only one direct parent class when inheriting, that is, single inheritance, but a class can implement multiple interfaces. Interfaces make up for the shortcomings of classes that cannot be multi-inherited. The double design of the interface not only maintains the data security of the class but also realizes multiple inheritance in disguise.

abstract class

In the Java language, when the abstract keyword is used to modify a class, the class is called an abstract class. An abstract class is a collection of common attributes of all its subclasses, and it is a class that contains one or more abstract methods. An abstract class can be seen as a further abstraction of a class. In the object-oriented field, abstract classes are mainly used for type hiding.

An abstract class can contain both general and abstract methods. The definition of an abstract method is different from that of a general method. An abstract method is directly followed by a semicolon after the method header, while a general method contains a method body enclosed in curly braces. All abstract methods must exist in the abstract class.

the difference

  1. Java abstract classes can provide partial implementation of certain methods, while Java interfaces cannot;
  2. When adding a new concrete method to an abstract class, all its subclasses will get the new method at once, and the Java interface cannot do this;
  3. The implementation of an abstract class can only be given by the subclass of this abstract class, that is to say, this implementation is in the inheritance hierarchy defined by the abstract class, and due to the single inheritance of the Java language;

    Any class that implements the methods specified by a Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so this class has multiple types.

use

In the synchronous method, the code can be executed sequentially in the method body to get the result and return it through return. This kind of method is generally not used for calculation, but only simple calculation code;

If the computing power is complex and takes a long time, use an asynchronous method instead of returning the result directly, and wait for the calculation to complete before calling back the result.

public class Demo {

    public static String reverseStrSync(String src) {
        StringBuilder temp = new StringBuilder();
        temp.append(src);
        temp.reverse();
        return temp.toString();
    }

    public static void reverseStrASync(String src, ResultListener resultListener, Result result) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder temp = new StringBuilder();
                temp.append(src);
                temp.reverse();
                if (resultListener != null) {
                    resultListener.reverse(temp.toString());
                }
                if (result != null) {
                    result.reverse(temp.toString());
                    result.reverse2(temp.toString());
                }
            }
        }).start();
    }

    public interface ResultListener {
        void reverse(String dst);
    }

    public abstract static class Result {
        abstract void reverse(String dst);

        void reverse2(String dst) {
            System.out.println("reverse2 = " + dst);
        }
    }



    public static void main(String[] args) {
        reverseStrASync("abcd", new ResultListener() {
            @Override
            public void reverse(String dst) {
                System.out.println("ResultListener reverse = " + dst);
            }
        }, new Result() {
            @Override
            void reverse(String dst) {
                System.out.println("Result reverse = " + dst);
            }
        });
    }
}

operation result

Guess you like

Origin blog.csdn.net/mozushixin_1/article/details/129030923
Recommended