Singleton txawv xwb ib lom yog tsim nyob rau hauv zov Threading

thaum uas siv cov kev txawv singleton, tsuas muaj ib lom yog tsim nyob rau hauv zov threading?

Siv cov chav kawm ntawv singleton threadsafe yuav lav tias tsuas muaj ib kev lom yog tsim.

tsev kawm ntawv ntim Singleton
{
lwm yam zoo li qub Singleton singleton = null;
zoo li qub readonly tsoom kwv singletonLock = tshiab kwv();

private Singleton() {}
pej xeem zoo li qub Singleton GetInstance()
{
ntsuas phoo (singletonLock)
{
Yog hais tias (singleton == null)
{
singleton = tshiab Singleton();
}
rov qab mus singleton ;
}
}
}

Qhov teeb meem no yuav tau tsa xwb thaum lub creation ntawm thawj lom.

Siv ntsuas phoo() yuav muab peb cov xov mus zoo yuav tsum tsis txhob tiav ntawm ob threads ib tib lub sij hawm los ua kev lom.

Saib meej dua peb cov txog qhov (singletonobject == null) ces nws yuav lav uas thaum lom yuav tsim tau.

muab ob npaug rau kos cov kev xaiv yuav tas nrho tej puavpheej ntawd tuaj rau peb kawm.

sau ntawv cia

Koj siv tau qhov cim npe no HTML

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>