— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

Put your thread to sleep 
if you want to be sure 
that other threads get a 
chance to run. 
When the thread wakes 
up, it always goes back 
to the runnable state 
and waits for the thread 
scheduler to choose it 
to run again.


502
 
chapter 15
Using sleep to make our program 
more predictable.
Remember our earlier example that kept giving us different 
results each time we ran it? Look back and study the code 
and the sample output. Sometimes main had to wait until the 
new thread finished (and printed “top o’ the stack”), while 
other times the new thread would be sent back to runnable 
before it was finished, allowing the main thread to come back 
in and print out “back in main”. How can we fix that? Stop 
for a moment and answer this question: “Where can you put 
a sleep() call, to make sure that “back in main” always prints 
before “top o’ the stack”?
We’ll wait while you work out an answer (there’s more than 
one answer that would work).
Figure it out? 
public class MyRunnable implements Runnable {
public void run() {
go();
}
public void go() {
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
ex.printStackTrace();
}

doMore();
}
public void doMore() {
System.out.println(“top o’ the stack”);
}
}
class ThreadTestDrive {
public static void main (String[] args) {
Runnable theJob = new MyRunnable();
Thread t = new Thread(theJob);
t.start();

Download 40,57 Mb.

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