Key uses of Java serials 109-sychronized and their notes, custom annotations

Keywords: Java github less Big Data

1. synchronized keywords

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

 

  //synchronized Keyword added to member method can synchronize memory variables

  public synchronized void withdraw(double money)  {

      double after = this.balance - money;

      try {

        //Here we delayed deliberately and we can see that the balance is wrong

        Thread.sleep(1000);

      }catch(InterruptedException e){

       

      }

      this.setBalance(after);

  }

 

 

These two methods have a wider range of keyword control, and more and more code in the future may be less efficient than precisely synchronized code blocks.

An example of a thread-safe class is StringBuffer\Vector\HashTable

Here's 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();

   

    //Delay (Guarantee) t1 Thread starts first and executes 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 void m1() {

    //dormancy

    try {

      Thread.sleep(1500);

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

    } catch(Exception w) {

     

    }

  }

  //m2 Method will wait m1 The method ends because t1 and t2 Shared a mc,also m1 and m2 All of the methods share an object with this keyword mc

  public synchronized void m2() {

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

  }

//  //m2 Method execution does not require waiting m1 End because m2 Not on method synchronized

//  public void m2() {

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

//  }

}

2. Use of System Notes

 

package com.bjpowernode.java_learning;

​

import java.util.LinkedList;

​

public class D109_2_SuperClass {

  public static void main(String[] args) {

    SuperClass109 superObj = new SuperClass109();

    superObj.MethodA();//Accessed obsolete methods, IDE Strikeout will be added

    System.out.println(superObj.var);//Access to obsolete domains with strikethrough

    SubClass109 subObj = new SubClass109();

    subObj.MethodB1();

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

    //The following comments suppress compilation warnings for the following statements

    @SuppressWarnings("rawtypes")

    LinkedList list = new LinkedList();

    //The following two statements are not added@SuppressWarnings,Warning message at compile time

    list.add(123);

    list.add("Beijing");

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

      System.out.println(list.get(i));

    }

   

  }

​

}

class SuperClass109{

  //Yes var Make a comment to indicate var Obsolete, though var Obsolete, but still usable

  @Deprecated

  int var = 125;

  @Deprecated

  public void MethodA() {

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

  }

  public void MethodB() {

    System.out.println("In parent class MethodB Method!");

  }

 

}

class SubClass109 extends SuperClass109{

  //@Override

  public void MethodB1(){

    System.out.println("Subclasses override methods in parent classes MethodB()!");

  }

}

​

3. Custom Notes

1. Custom annotations to be declared with @interface automatically inherit the java.lang.annotation.Annotation interface

2. When defining custom annotations, you cannot inherit other annotations and interfaces. @interface is used to declare only one annotation, and each method in the annotation is actually known as a configuration parameter.

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

4. Grammar Format

 

[public|final] @interface annotation name {

  Annotation Elements

}

 

 

The keyword @Interface declares a custom comment, the comment name is a legal identifier, the comment element is a parameterless method, and the type of method is white Oh the type of the comment element.

Syntax format of annotation elements

 

Data type comment element name () [default default]

 

 

If there is only one annotation element, in the case of the annotation element name value, you can use it without writing out the annotation element name, just give the annotation value directly.When using custom annotations, free the custom annotations from the previous or the same line where they are needed, and love to write the values of the annotation elements in parentheses after the custom annotations.If the default value is used, the paper media may not be given. If there is only one comment element and it is named value, it only needs to be given a value, not a comment element name.

4. 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 the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Posted by michaelphipps on Sun, 12 Apr 2020 09:57:05 -0700