— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet477/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   473   474   475   476   477   478   479   480   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

516
 
chapter 15
The deadly side of synchronization
Be careful when you use synchronized code, because nothing 
will bring your program to its knees like thread deadlock. 
Thread deadlock happens when you have two threads, both of 
which are holding a key the other thread wants. There’s no way 
out of this scenario, so the two threads will simply sit and wait. 
And wait. And wait.
If you’re familiar with databases or other application servers, 
you might recognize the problem; databases often have a 
locking mechanism somewhat like synchronization. But a 
real transaction management system can sometimes deal with 
deadlock. It might assume, for example, that deadlock might 
have occurred when two transactions are taking too long to 
complete. But unlike Java, the application server can do a 
“transaction rollback” that returns the state of the rolled-back 
transaction to where it was before the transaction (the atomic 
part) began. 
Java has no mechanism to handle deadlock. It won’t even know 
deadlock occurred. So it’s up to you to design carefully. If you 
find yourself writing much multithreaded code, you might 
want to study “Java Threads” by Scott Oaks and Henry Wong 
for design tips on avoiding deadlock. One of the most common 
tips is to pay attention to the order in which your threads are 
started.
A simple deadlock scenario:
A
Thread A enters a 
synchronized method 
of object 
foo
, and gets 
the key. 
Thread A goes to 
sleep, holding the 
foo
key.
B
Thread B enters a 
synchronized method 
of object 
bar
, and gets 
the key.
Thread B tries to enter 
a synchronized method 
of object 
foo
, but can’t 
get 
that 
key (because 
A has it). B goes 
to the waiting lounge, 
until the
 foo
key is 
available. B keeps the 
bar
key.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   473   474   475   476   477   478   479   480   ...   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