— Scott McNealy, Sun Microsystems Chairman, President and ceo


But first he falls asleep!



Download 40,57 Mb.
Pdf ko'rish
bet466/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   462   463   464   465   466   467   468   469   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

But first he falls asleep!
And that’s where 
I come in, while Ryan’s still asleep, and 
now 
I want to withdraw $100. I check the balance, and 
it’s $100 (because Ryan’s still asleep and hasn’t yet made 
his withdrawal), so I think, no problem. So I make the 
withdrawal, and again no problem. But then Ryan wakes up, 
completes 
his withdrawal, and we’re suddenly overdrawn! He didn’t 
even know that he fell asleep, so he just went ahead and completed his 
transaction without checking the balance again. You’ve got to help us Dr. 
Steve!”
Is there a solution? Are they doomed? We can’t stop Ryan from falling 
asleep, but can we make sure that Monica can’t get her hands on the bank 
account until after he wakes up? 
Take a moment and think about that while we go to a commercial break.
Ryan falls asleep after 
he checks the balance 
but before he makes the 
withdrawal. When he wakes 
up, he immediately makes 
the withdrawl without 
checking the balance again.
Ryan and Monica: victims 
of the “two people, one 
account” problem.


506
 
chapter 15
The Ryan and Monica problem, in code
The following example shows what can happen when two 
threads (Ryan and Monica) share a single object (the bank 
account).
The code has two classes, BankAccount, and 
MonicaAndRyanJob. The MonicaAndRyanJob class 
implements Runnable, and represents the behavior that Ryan 
and Monica both have—checking the balance and making 
withdrawals. But of course, each thread falls asleep 
in between 
checking the balance and actually making the withdrawal.
The MonicaAndRyanJob class has an instance variable of type 
BankAccount., that represents their shared account.
The code works like this:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   462   463   464   465   466   467   468   469   ...   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