— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

510
 
chapter 15
We need the makeWithdrawal ( ) method 
to run as one 
atomic
thing. 
 
private 
synchronized
 void makeWithdrawal(int amount) {
if (account.getBalance() >= amount) {
System.out.println(Thread.currentThread().getName() + “ is about to withdraw”);
try {
System.out.println(Thread.currentThread().getName() + “ is going to sleep”);
Thread.sleep(500);
} catch(InterruptedException ex) {ex.printStackTrace(); }
System.out.println(Thread.currentThread().getName() + “ woke up.”);
account.withdraw(amount);
System.out.println(Thread.currentThread().getName() + “ completes the withdrawl”);
} else {
System.out.println(“Sorry, not enough for “ + Thread.currentThread().getName());
}
 }
The 
synchronized 
keyword means that 
a thread needs a key 
in order to access the 
synchronized code.
To protect your 
data 
(like the bank account), 
synchronize the 
methods that act on 
that data.
We need to make sure that once a thread enters the 
makeWithdrawal() method, it must be allowed to finish the method 
before any other thread can enter.
In other words, we need to make sure that once a thread has 
checked the account balance, that thread has a guarantee that it can 
wake up and finish the withdrawal before any other thread can check the 
account balance!
Use the 
synchronized 
keyword to modify a method so that only 
one thread at a time can access it.
That’s how you protect the bank account! You don’t put a lock on 
the bank account itself; you lock the method that does the banking 
transaction. That way, one thread gets to complete the whole 
transaction, start to finish, even if that thread falls asleep in the 
middle of the method!
So if you don’t lock the back account, then what exactly 
is locked? Is 
it the method? The Runnable object? The thread itself?
We’ll look at that on the next page. In code, though, it’s quite 
simple—just add the synchronized modifier to your method 
declaration:
(Note for you physics-savvy readers: yes, the convention of using the word ‘atomic’ here does not reflect 
the whole subatomic parcticle thing. Think Newton, not Einstein, when you hear the word ‘atomic’ in the 
context of threads or transactions. Hey, it’s not OUR convention. If WE were in charge, we’d apply 
Heisenberg’s Uncertainty Principle to pretty much everything related to threads.)

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   467   468   469   470   471   472   473   474   ...   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