If the object of your design is in the program,have an example only, namely of Singleton, have a kind of feasible settlement method will realize its LazyLoad: Use kind of to load namely implement LazyLoad is characteristic. Code is as follows:
Class ResSingleton {public Static Resource Res = New Resource();}
ResSingleton has variable of member of a static state only here. When using ResSingleton.res for the first time, JVM just is met initialization example of a Resource, and JVM can make sure the result of initialization is seasonable write advocate put, can let other line Cheng see, succeed so realized LazyLoad.
Besides this method, return the method that can use ThreadLocal to realize DCL, but the implementation efficiency as a result of ThreadLocal is lower, so this kind solves method to be able to have bigger performance loss, the reader that have fun at can consult the reference material after article.
What want a specification finally is, effective to DCL, the individual thinks more it is one kind is contained conclude and discuss of scholastic gas. And in light of the angle from rationalism, the variable that accesses any likelihoods to share (the object cites) need synchronous protection, make mistake likely otherwise, but can increase what lock up to death to produce odds again with Synchronized everywhere, how the programmer of bitter life resolves this contradiction? In fact, source project opens in a lot of Java (for instance Ofbiz/Jive) the evidence that use DCL can find in code, I also had not come up against the program that produces because of DCL in specific practice unusual. The individual's preference is: Might as well first bold use DCL, wait for Synchronized of occurrence problem reoccupy to eliminate stage by stage. Perhaps somebody slants at guarding, think stability is crushing everything, that mights as well rise with Synchronized synchronism first, I think this is the problem of an opinions differ from each other, and decision of the ability after be aimed at specific project concrete analysis. Still a method writes case of a test to check a system to whether be put in DCL phenomenon namely, a such example were provided in accessary CD, interested reader can compile a test by oneself. Without giving thought to result how, such discussion the understanding JMM with conduce to us better, nurturance analyses the habit of the problem with the train of thought of multi-line Cheng, the programming that improves us ability.
3, synchronism of Java line Cheng enhances a bag
Believe you had understood Java to use at synchronous 3 broad ax: Synchronized/wait/notify, they are simple really and effective. But fall in certain circumstance, we need more complex synchronous tool. A little simple synchronous tool kind, such as ThreadBarrier, semaphore, readWriteLock, can him process designing comes true. What want the introduction now is the Concurrent bag of Niu Ren Doug Lea. This bag is implementation Java technically development of place of advanced and collateral program, can satisfy the requirement of our majority. More exciting is, this bag publishs source code, can download freely. And in JDK1.5 this packets will serve as SDK to offer Java to develop personnel partly.
Previous 1 2 3 4 56 7 8 Next