— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet461/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   457   458   459   460   461   462   463   464   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

networking 
and
threads
you are here
4
501
Putting a thread to sleep
One of the best ways to help your threads take turns is 
to put them to sleep periodically. All you need to do 
is call the static sleep() method, passing it the sleep 
duration, in milliseconds.
For example:
Thread.sleep(2000);
will knock a thread out of the running state, and 
keep it out of the runnable state for two seconds. 
The thread can’t become the running thread 
again until after at least two seconds have passed. 
A bit unfortunately, the sleep method throws an 
InterruptedException, a checked exception, so all 
calls to sleep must be wrapped in a try/catch (or 
declared). So a sleep call really looks like this:
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
Your thread will probably never be interrupted from 
sleep; the exception is in the API to support a thread 
communication mechanism that almost nobody uses in 
the Real World. But, you still have to obey the handle 
or declare law, so you need to get used to wrapping your 
sleep() calls in a try/catch.
Now you know that your thread won’t wake up before the 
specified duration, but is it possible that it will wake up 
some time after the ‘timer’ has expired? Yes and no. It 
doesn’t matter, really, because when the thread wakes 
up, it always goes back to the runnable state! The thread 
won’t automatically wake up at the designated time and 
become the currently-running thread. When a thread 
wakes up, the thread is once again at the mercy of 
the thread scheduler. Now, for applications that don’t 
require perfect timing, and that have only a few threads, 
it might appear as though the thread wakes up and 
resumes running right on schedule (say, after the 2000 
milliseconds). But don’t bet your program on it. 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   457   458   459   460   461   462   463   464   ...   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