Detailed explanation of Java multithreading keywords final and static

This article mainly introduces the detailed explanation of Java multithreading keywords final and static. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it

Final keyword

1. Features of final keyword in single thread:

1) Static member of final modification: it must be assigned during display initialization or static code block, and can only be assigned once.

2) The class member variable modified by final can be assigned in three places: display initialization, construction code block and construction method, and can only be assigned once.

3) The local variable modified by final must be displayed and initialized before use (not necessarily assigned in the definition), and can only be assigned once.

In short, the static members of final modification must be assigned before the class is loaded, and the member variables of final modification must be assigned before the object is created.

Supplement: the basic data type modified by final cannot be re assigned. For the reference data type modified by final, the address of the reference type cannot be changed, and the internal properties of the reference type can be changed.

2. The role of final in multithreading

Due to the effect of reordering, when a thread reads the reference of an object, the object may not have been initialized, that is, these threads may read the default value of the object field instead of the initial value.

The final keyword has a special function: when an object is published to other threads, all final fields of the object are initialized, that is, what other threads read is the initial value of the corresponding field rather than the default value. There is no such guarantee for non final fields.

For the field of reference type modified by final, this field can also ensure that the object referenced by this field is initialized.

This mechanism is implemented by prohibiting reordering:

For the writing of final variables: it is forbidden to reorder the writing of the final field outside the construction method, that is, when an object reference is obtained, the internal final field must have been initialized.

For the reading of the final field: it is forbidden to reorder the reference of the object for the first time and the final field contained in the object.

These two kinds of reordering are implemented through the memory barrier at the bottom. The compiler will insert a storestore barrier after the final field is written, before the constructor is executed, and a loadload barrier before reading the final field.

Static keyword

Static keyword can ensure that a thread can always read the initial value of a class static variable even without using other thread synchronization mechanisms, but this visibility guarantee is limited to reading the variable for the first time

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>