— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet472/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   468   469   470   471   472   473   474   475   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

using 
synchronized


networking 
and
threads
you are here
4
511
Using an object’s lock 
Every object has a lock. Most of the time, the 
lock is unlocked, and you can imagine a virtual 
key sitting with it. Object locks come into play 
only when there are synchronized methods. 
When an object has one or more synchronized 
methods, a thread can enter a synchronized 
method only if the thread can get the key to the 
object’s lock!
The locks are not per method, they 
are per object. If an object has two 
synchronized methods, it does not 
simply mean that you can’t have two 
threads entering the same method. It 
means you can’t have two threads entering 
any of the synchronized methods.
Think about it. If you have multiple 
methods that can potentially act on an 
object’s instance variables, all those methods 
need to be protected with synchronized.
The goal of synchronization is to protect 
critical data. But remember, you don’t lock the 
data itself, you synchronize the methods that 
access that data. 
So what happens when a thread is cranking 
through its call stack (starting with the run() 
method) and it suddenly hits a synchronized 
method? The thread recognizes that it needs 
a key for that object before it can enter the 
method. It looks for the key (this is all handled 
by the JVM; there’s no API in Java for accessing 
object locks), and if the key is available, the 
thread grabs the key and enters the method. 
From that point forward, the thread hangs on 
to that key like the thread’s life depends on 
it. The thread won’t give up the key until it 
completes the synchronized method. So while 
that thread is holding the key, no other threads 
can enter any of that object’s synchronized 
methods, because the one key for that object 
won’t be available.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   468   469   470   471   472   473   474   475   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish