— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

1
2
A
B
foo
bar
foo
bar
A
Thread A wakes up (still 
holding the 
foo 
key) 
and tries to enter a 
synchronized method on 
object 
bar
, but can’t get 
that 
key because B has 
it. A goes to the waiting 
lounge, until the 
bar 
key is 
available (it never will be!)
Thread A can’t run until 
it can get the 
bar 
key, 
but B is holding the 
bar 
key and B can’t run until it 
gets the 
foo 
key that A is 
holding and...
3
A
B
A
All it takes for 
deadlock are two 
objects and two 
threads.
foo
bar
foo
thread 
deadlock


networking 
and
threads
you are here
4
517
 BULLET POINTS
ß
The static Thread.sleep() method forces a thread to leave the 
running state for at least the duration passed to the sleep method. 
Thread.sleep(200) puts a thread to sleep for 200 milliseconds. 
ß
The sleep() method throws a checked exception (InterruptedException), 
so all calls to sleep() must be wrapped in a try/catch, or declared.
ß
You can use sleep() to help make sure all threads get a chance to run, 
although there’s no guarantee that when a thread wakes up it’ll go to the 
end of the runnable line. It might, for example, go right back to the front. 
In most cases, appropriately-timed sleep() calls are all you need to keep 
your threads switching nicely. 
ß
You can name a thread using the (yet another surprise) setName() 
method. All threads get a default name, but giving them an explicit name 
can help you keep track of threads, especially if you’re debugging with 
print statements.
ß
You can have serious problems with threads if two or more threads have 
access to the same object on the heap. 
ß
Two or more threads accessing the same object can lead to data 
corruption if one thread, for example, leaves the running state while still 
in the middle of manipulating an object’s critical state.
ß
To make your objects thread-safe, decide which statements should be 
treated as one atomic process. In other words, decide which methods 
must run to completion before another thread enters the same method 
on the same object.
ß
Use the keyword 
synchronized 
to modify a method declaration, 
when you want to prevent two threads from entering that method. 
ß
Every object has a single lock, with a single key for that lock. Most of the 
time we don’t care about that lock; locks come into play only when an 
object has synchronized methods.
ß
When a thread attempts to enter a synchronized method, the thread 
must get the key for the object (the object whose method the thread 
is trying to run). If the key is not available (because another thread 
already has it), the thread goes into a kind of waiting lounge, until the key 
becomes available. 
ß
Even if an object has more than one synchronized method, there is still 
only one key. Once any thread has entered a synchronized method on 
that object, no thread can enter any other synchronized method on the 
same object. This restriction lets you protect your data by synchronizing 
any method that manipulates the data.



Download 40,57 Mb.

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