About the misuse of synchronization and interface in Java

Source: Internet
Author: User

Recently in writing a mobile phone small application System Business module There are some of the abstraction and interface planning is mainly the interface part of the same or the same nature of things we will pull away from the implementation of the interface unified for the business to achieve a more flexible use of the node.


Topics are as follows

How do I declare that the method described by synchronization is feasible in interface


In fact, the previous versions of Java 1.2 were allowed to do so.

public interface demointerface{synchronization void Function1 ();}

However, after 1.2, the version will not work and it is wrong to do so. Interface (interface) is just like the name of the "They are all elevators," but either the method or the physical structure is not necessarily the same, even if the open method is the same button but the implementation is just the same as the same image or call a button called "switch" can be started. So the meaning of the interface is to let people do not need to know too much detail and just know a purely role---"open" on the line.


Speaking of which, there may be a shoe boy asking what is the difference between the use of abstract class and the interface in fact, it depends on how you plan to decide when and where to use the abstract or interface topic. Simple explanations have a common and consistent basic implementation when using abstract so that the implementation of the class less code writing volume of the division of labor is also easy to maintain and control, etc. and need to take care of the broader use of the situation and there is no need to stipulate the implementation of the basic form of the class use interface best.


Back to the point my requirement is that there are several algorithms implemented that have common use of the Ingress interface Interface but not all implementations need to consider thread synchronization, of course, it is also possible to uniformly request thread synchronization you will find that you write an abstract class directly to inherit it just as the previous claim is only the same as the entry of the same, but the implementation method is not necessarily the same or partially the same as the interface name but does not require thread synchronization.

As follows

public abstract demoabstract{synchronization void Function1 (); Synchronization Boolean function2 () {//ToDo ...}} or public interface demointerface{synchronization void Function1 ();}

If this is the right thing to say, first of all, it's a violation of the abstract. We do this in the abstract, the difference between the implementation of their agreement is not as good as directly write the implementation of the class well, there is no need to abstract you say right?


The following instructions were found in the reading of some materials

< Span style= "line-height:0px;" > although the  synchronized keyword can appear in a method header, it does not form a part of a method ' s API or cont Ract.


The key is that the red-bottom part of the text shows that all synchronization should be used in the implementation class to modify the specific implementation method rather than in the interface.


Here is a look for you and yourself to review

Interface public interface Scientifictheory {void publish ();    void predict ();  Boolean falsifythroughmeasurement (); }
//  implementation class  LoopQuantumGravity1public final class  loopquantumgravity2 implements scientifictheory {   @Override   public  Synchronized void publish ()  {    //. elided  }       @Override  //  thread synchronization required   public  Synchronized void predict ()  {    //. elided  }       @Override    public boolean  Falsifythroughmeasurement ()  {    return true; //stub   }   } 
Implementation Class Loopquantumgravity2public final class LoopQuantumGravity2 implements Scientifictheory {@Override public synch ronized void Publish () {//. elided} @Override//Do not require thread synchronization public void predict () {//: elided} @Override public boolean falsifythroughmeasurement () {return true;//stub}}


About the misuse of synchronization and interface in Java

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.