Position:Home>JAVA Development> Java line Cheng / the blemish of memory model and increase
Java line Cheng / the blemish of memory model and increase
From;  Author:Stand originally


What we are in front is paragraphic the action that introduced Synchronized, now, will examine Synchronized key word afresh from the angle of JMM.
Assume some line Cheng implements code of a Synchronized paragraph, meantime undertakes operating to some variable, JVM will be ordinal carry out following movements:
(1) gets synchronous object Monitor (lock)
(2) from advocate put duplicate variable to current job memory (Read And Load)
(3) implements code, the change shares variable cost (Use And Assign)
(4) uses break of working memory data advocate put relevant content (Store And Write)
(5) releases synchronous object to lock up (Unlock)
Visible, the another action of Synchronized is to assure advocate the consistency of data in the working memory that keeps content and line Cheng. If do not have use Synchronized key word, JVM does not make sure the 2nd pace is met with the 4th pace carry out strictly instantly according to afore-mentioned order. Because set mediumly according to JLS, the working memory of line Cheng and advocate putting the data between to exchange is loose coupling, when to need renovation job memory to perhaps be updated advocate memory content, can realize write one's own ticket by particular fictitious machine. The code that if many lines Cheng carries out a paragraph at the same time,did not protect via Synchronized paragraph, very possible some line Cheng had altered variable value, but other line Cheng cannot see this alters however, still be worth aspirant travel operation in old variable, bring about the operation result that cannot expect finally.


2, DCL invalidation

This one what we should discuss is a topic that makes Java humiliating: DCL invalidation. Before beginning to discuss, introduce LazyLoad first, this kind of skill is very commonly used, point to namely kind include variable of a certain member, be in kind of initialization when do not be this variable initialization instantly an example, however when when wanting to use this variable truly of ability initialization.
Issue the code of the face for example:
Code 1
Class Foo {Private Resource Res = Null; Public Resource GetResource() {If (res==Null) Res = New Resource(); Return Res; }}

Because LazyLoad can reduce systematic natural resources effectively to use up, improve the performance of program whole, so by extensive use, the default that connects Java kind to load implement also use this kind of method to come to load Java kind.
Below single-track Cheng environment, everything Dou Xiangan does not have a thing, but if put the code above to multi-line Cheng environment next moving, may appear so problem. Have 2 lines Cheng suppose, carried out If(res at the same time==Null) , so very possible Res by initialization 2, to avoid such Race Condition, use Synchronized key word rises the methodological synchronism above. Code is as follows:
Code 2

Class Foo {Private Resource Res = Null; Public Synchronized Resource GetResource() {If (res==Null) Res = New Resource(); Return Res; }}
Previous 1 23 4 5 6 7 8 Next