Java serialization 109-sychronized key uses and their attention points, custom annotations

1. The synchronized keyword

1. Let's modify the withdraw method in the last serialization

 

  // The synchronized keyword is added to the member method to achieve the purpose of synchronizing memory variables. 

  public  synchronized  void withdraw ( double money) { 

      double after = this .balance- money; 

      try { 

        // Here we deliberately delayed a bit, we can see the balance wrong 

        the Thread.sleep ( 1000 ); 

      } the catch (InterruptedException E) { 

       

      } 

      the this .setBalance (After); 

  }

 

 

These two methods have a larger keyword control range, and more and more codes in the future may reduce efficiency, which is not as efficient as code blocks that are precisely synchronized.

Examples of thread-safe classes: StringBuffer \ Vector \ HashTable

Let me explain a case

 

package com.bjpowernode.java_learning;

​

public class D109_1_SynchronizedMechanism {

  public static void main(String[] args) throws InterruptedException{

    MyClass109 mc = new MyClass109();

    Processer109 p = new Processer109(mc);

    Thread t1 = new Thread(p);

    Thread t2 = new Thread(p);

    t1.setName("t1");

    t2.setName("t2");

    t1.start();

   

    //延迟(保证t1线程先启动,并执行run)

    Thread.sleep(1000);

    t2.start();

  }

​

}

class Processer109 implements Runnable{

  MyClass109 mc;

  public Processer109(MyClass109 mc){

    this.mc = mc;

  }

  public void run() {

    if(Thread.currentThread().getName().equals("t1")) {

      mc.m1();

    }

    if(Thread.currentThread().getName().equals("t2")) {

      mc.m2();

    }

  }

}

class MyClass109{

  public synchronized voidm1 () { 

    // sleep 

    try { 

      Thread.sleep ( 1500 ); 

      System.out.println ( "m1 ....." ); 

    } catch (Exception w) { 

     

    } 

  } 

  // m2 method will wait for m1 method to end , Because t1 and t2 share a mc, and this keyword on both m1 and m2 methods, share an object mc 

  public  synchronized  void m2 () { 

    System.out.println ( "m2 ......" ); 

  } 

//   // m2 method execution does not need to wait for m1 to end, because m2 method is not synchronized 

//   public void m2 () { 

//     System.out.println ("m2 ......"); 

//   } 

}

Second, the use of system annotations

 

Package com.bjpowernode.java_learning; 


Import java.util.LinkedList; 


public  class D109_2_SuperClass { 

  public  static  void main (String [] args) { 

    SuperClass109 superObj = new new SuperClass109 (); 

    superObj.MethodA (); // accessing obsolete The method, IDE will add a strikethrough 

    System.out.println (superObj.var); // Access the outdated domain, will also add a strikethrough 

    SubClass109 subObj = new SubClass109 (); 

    subObj.MethodB1 (); 

    // - ------------ 

    // The following annotation is used to suppress the compilation warning message of the statement below 

    @SuppressWarnings ("rawtypes" ) 

    LinkedList list = new LinkedList (); 

    // The following two statements do not add @SuppressWarnings, warning message will appear during compilation 

    list.add ( 123 ); 

    list.add ( "Beijing" ); 

    for ( int i = 0; I <2; I ++ ) { 

      System.out.println (List.get (I)); 

    } 

   

  } 
} 



class SuperClass109 { 

  // to annotate var, var represents outdated, though var outdated, but may still Use 

  @Deprecated 

  int var = 125 ; 

  @Deprecated 

  public  void MethodA () { 

    System.out.println ("Method () method in parent class!" ); 

  } 

  Public  void MethodB () { 

    System.out.println ( "MethodB method in parent class!" ); 

  } 

 

} 

Class SubClass109 extends SuperClass109 { 

  // @Override 

  public  void MethodB1 () { 

    System.out.println ( "method MethodB subclasses override the parent class ()!" ); 

  } 

}

3. Custom annotations

1. Custom annotations should be declared using @interface, which will automatically inherit the java.lang.annotation.Annotation interface

2. When defining custom annotations, you cannot inherit other annotations and interfaces. @Interface is only used to declare an annotation. Each method in the annotation is actually a configuration parameter.

3. The name of the method is the name of the parameter, and the return type is the type of the parameter. The type of the return value can only be the basic data type, Class, String, Enum. The default value of the parameter can be declared through the default keyword.

4. Grammatical format

 

[ public | final ] @ interface    annotation name { 

  annotation element 

}

 

 

Among them, the keyword @Interface means to declare a custom annotation, "annotation name" is a legal identifier, "annotation element" is a parameterless method, the type of method is the type of the annotation element.

Annotation element syntax format

 

Data type annotation element name () [ default    ]

 

 

If there is only one annotation element, in the case where the annotation element is named value, it is not necessary to write out the annotation element name when it is used, just need to directly give the annotation value. When using custom annotations, liberate the custom annotations in the previous line or the same line that requires the annotations, and write the value of the annotation element in parentheses after the custom annotations. If you use the default value, you can not give the paper media. If there is only one annotation element and the value is named, you only need to give the value instead of giving the annotation element name.

Fourth, the source code:

D109_1_SynchronizedMechanism.java

D109_2_SuperClass.java

https://github.com/ruigege66/Java/blob/master/D109_1_SynchronizedMechanism.java

https://github.com/ruigege66/Java/blob/master/D109_2_SuperClass.java

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to pay attention to WeChat public number: Fourier transform, personal public number, only used for learning and communication, reply "gift pack" in the background, get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12688684.html