Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Talking about the understanding of java

Shulou Source: shulou.com Published: 2022-06-03 06:05:19 09月24日 Update

1. Java compiles once and executes everywhere-cross-platform

2 、 gc

3. The difference between jre and jdk:

Jre (java runtime environment) java runtime environment, including jvm's client, class libraries (rt.jar: commonly used collections, io, math, lang, etc. Are all in the rt.jar package)

Jdk (java development kit) java development tools, including jre (client and server with jvm), compiler (javac), diagnostic tools (jconsole.exe, etc.)

4. Java basic class library: (beginning with java)

Java.lang (wrapper classes such as exception/string/stringbuffer/Integer / math/annotion)

Java.util (concurrent/regex regular expression / collection/ date date calendar)

Reference https://blog.csdn.net/basycia/article/details/50769838

5. Third-party class libraries commonly used in java: (beginning with javax)

Javax.swing 、 javax.naming

6. Cas principle

The classes under the java.util.concurrent package all use cas principles to achieve concurrency, such as automicInteger and concurrentHashMap

Main memory A, thread cache data B, update value C, and C is assigned to An only when BackA is used.

7. Concurrency feature, which can be implemented by syn or lock.

Atomicity

Orderliness: when jvm executes java code, it will rearrange instructions without affecting the running results. Orderliness is to avoid instruction rearrangement.

Memory visible

8. Volatile keyword

Can ensure that the memory is visible: thread A writes the data to the main memory immediately after modification.

Ordering: modified keywords are not rearranged by instructions. Ensure that when performing keyword-related operations, the previous code has been executed and the subsequent code has not been executed, but the sequence of the code before and after the keyword is not guaranteed.

There is no guarantee of atomicity.

A typical application scenario of volatile is singleton mode.

Class Singleton {private volatile static Singleton instance= null; private Singleton () {} public static Singleton getInstance () {if (instance==null) {synchronized (Singleton.class) {if (instance==null) instance= new Singleton ();}} return instance;}}

Why does the singleton pattern need to be modified with volatile?

It mainly lies in the sentence instance = new Singleton (), which is not an atomic operation. In fact, this sentence in JVM does about three things:

1. Allocate memory to instance

two。 Call the constructor of Singleton to initialize member variables

3. Point the instance object to the allocated memory space (instance will be non-null after performing this step).

But there is an optimization for instruction reordering in JVM's just-in-time compiler. In other words, the order of the second and third steps above cannot be guaranteed, and the final execution order may be 1-2-3 or 1-3-2. If it is the latter, it is preempted by thread 2 before the execution of 3 and 2, and the instance is already non-null (but not initialized), so thread 2 will directly return instance, then use it, and then naturally report an error.

Reference:

Do you really know the volatile keyword?

Deep and simple CAS

Tags: Guarantees keys keywords memory code instructions threads order atoms ordering order compilation principles tools common use beginnings data patterns compilers allocation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology OPPO Reno MySQL Huawei