— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet486/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   482   483   484   485   486   487   488   489   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

526
 
chapter 15
Exercise Solutions
public class TestThreads {
public static void main(String [] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
Thread one = new Thread(t1);
Thread two = new Thread(t2);
one.start();
two.start();
}
}
class Accum {
private static Accum a = new Accum();
private int counter = 0;
private Accum() { }
public static Accum getAccum() {
return a;
}
public void updateCounter(int add) {
counter += add;
}
public int getCount() {
return counter;

}
class ThreadOne implements Runnable {
Accum a = Accum.getAccum();
public void run() {
for(int x=0; x < 98; x++) {
a.updateCounter(1000);
try {
Thread.sleep(50);
} catch(InterruptedException ex) { }
}
System.out.println(“one “+a.getCount());
}
}
class ThreadTwo implements Runnable {
Accum a = Accum.getAccum();
public void run() {
for(int x=0; x < 99; x++) {
a.updateCounter(1);
try {
Thread.sleep(50);
} catch(InterruptedException ex) { }
}
System.out.println(“two “+a.getCount());
}
}
Threads from two different classes are updating 
the same object in a third class, because both 
threads are accessing a single instance of Accum.
The line of code:
private static Accum a = new Accum( );
creates a 
static instance of Accum (remember static means 
one per class), and the private constructor in 
Accum means that no one else can make an Accum 
object. These two techniques (private constructor 
and static getter method) used together, create 
what’s known as a ‘Singleton’ - an OO pattern to 
restrict the number of instances of an object 
that can exist in an application. (Usually, there’s 
just a single instance of a Singleton—hence the 
name), but you can use the pattern to restrict the 
instance creation in whatever way you choose.)
create a static instance 
of class Accum
A private constructor

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   482   483   484   485   486   487   488   489   ...   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