Peb puas tuaj yeem siv hashmap nyob rau hauv ib puag ncig sib txuas?

Cov txheej txheem:

Peb puas tuaj yeem siv hashmap nyob rau hauv ib puag ncig sib txuas?
Peb puas tuaj yeem siv hashmap nyob rau hauv ib puag ncig sib txuas?
Anonim

Dab tsi yog qhov siv HashMap hauv ib puag ncig sib txuas? … Nws yog ib qho teeb meem yog tias ntau cov xov ntxiv rau tib yam HashMap yam tsis muajnws raug synchronized. Txawm hais tias tsuas yog 1 xov hloov kho HashMap thiab lwm cov xov tau nyeem los ntawm tib daim ntawv qhia tsis muaj synchronization, koj yuav khiav mus rau qhov teeb meem.

Koj puas tuaj yeem siv HashMap hauv ib puag ncig sib txuas?

Koj yuav tsum paub tseeb tias: Txhua qhov hloov tshiab rau HashMap tau ua tiav ua ntej cov xov tam sim ntawd thiab cov xov uas tsim daim ntawv qhia kuj forks cov xov. Cov xov tsuas yog siv HashMap hauv hom nyeem nkaus xwb- txawm tias tau txais lossis rov ua dua yam tsis tau tshem tawm. Tsis muaj xov xwm hloov kho daim ntawv qhia.

Vim li cas HashMap yuav tsum tsis txhob siv nyob rau hauv ib puag ncig multithreaded tuaj yeem ua rau lub voj tsis kawg ib yam?

Lub peev xwm ntawm HashMap yog 16 thiab Load factor yog 0.75, uas txhais tau hais tias HashMap yuav muab ob npaug rau nws lub peev xwm thaum 12th Key-Value khub nkag rau hauv daim ntawv qhia (160.75=12). Thaum 2 xov sim nkag mus rau HashMap ib txhij, ces koj yuav ntsib qhov tsis kawg. Xov xwm 1 thiab xov 2 sim muab 12 tus yuam sij-tus nqi.

HashMap puas tau txais xov zoo?

HashMap tsis yog synchronized. Nws tsis yog xov-kev nyab xeeb thiab tsis tuaj yeem sib koom ntawm ntau cov xov yam tsis muaj kev sib txuas kom raug cai whereas Hashtable yog synchronized.

Dab tsi yog qhov zoo tshaj plaws rau ntau txoj xovib puag ncig?

Cov lus teb yog " ConcurrentHashMap"

Pom zoo: