Position:Home>JAVA Development> The blemish of Java line Cheng
The blemish of Java line Cheng
From;  Author:Stand originally

Fixity (Immutability)

Because do not need synchronism to the visit of constant target, fall in multi-line Cheng condition so, changeless concept (the value of an object cannot be changed after found) it is impayable. In utterance of Java process designing, not quite strict to the implementation of fixity, have two reasons: To a constant target, before its are founded completely by unfinished, can have a visit to it. This kind of visit can produce incorrect cost to certain region. To constant (kind the definition that all region are Final) is too inattentive. The target that specifies to citing by Final, although cite itself is unalterable, but object itself can change position.

The first problem can be solved, do not allow line Cheng to begin to carry out in tectonic function (perhaps cannot carry out before tectonic function is returned begin to request) .

To the 2nd problem, through demarcate Final got-up accord with is pointed to constant object, can solve this problem. This that is to say, to an object, only all region are Final, and the region of all quotative objects also is Final, this object just is constant truly. To do not break existing code, this definition can be used compile implement strengthen, have only namely kind be by explicit mark changeless when, this kind of ability is changeless kind. The method is as follows:

After having $immutable to decorate accord with, it is optional that the Final in region definition decorates accord with.

Finally, should use interior kind (after Inner Class) , compile in Java implement a medium mistake makes it cannot establish constant target reliably. Become kind of interior with have important kind when (my code often has) , compile implement often display following and wrong message incorrectly:

Make empty Final has initialization in every tectonic function already, still can appear this wrong message. Since interior introduces in 1.1 version kind hind, compile implement in have this is wrong all the time. Version is here medium (after 3 years) , this mistake still exists. Now, should be when correcting this error.

To kind the example class visit of class region

Besides visitting attributive, still have a problem, namely kind class (static state) method and example (blame static state) the method can visit kind of class directly (static state) region. This kind of visit is breakneck, because the synchronism of example method won't get the lock of kind of class, so method of a Synchronized Static and method of a Synchronized still can be visited at the same time kind region. A when correct this problem apparent method is, the requirement uses talent of Static access method only in example method visit blame is changeless kind Static region. Of course, this kind of requirement needs to compile implement check with run time. In this kind the regulation falls, the code below is illegal:

Because F() and G() can run paralell,move, the value that so they can change X at the same time (the result that produces adventitious) . Remember please, here has two locks: Static method asks to belong to the lock of Class object, method of static state of and rather than asks to belong to the lock of this kind of example. Should be not changeless Static from the visit in example method when region, compile implement should ask to satisfy below the random in two structures one:
Previous 1 2 3 4 5 6 7 8 9 1011 12 13 Next