File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about thread at J@Whiz1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about thread at J@Whiz1.4" Watch "question about thread at J@Whiz1.4" New topic

question about thread at J@Whiz1.4

dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
Follow is the question:
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?
class MyClass
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount()
return ++myCount;

public void getYourNumber()
yourNumber = nextCount();
C) Each thread will get a unique number.
D) The uniqueness of the number among different Threads can't be guaranteed.
the correct answer is C),but I think it should be D),because if one thread want to get the mycount,it must initial an instanse of class MyClass,eg,mc,then mc.yourNumber.but at that time other thread may modified the value of yourNumber not use the method,
please help me!thanx.

scjp 1.4<br />challenge haven't limit!
Chloe Loh

Joined: Aug 27, 2002
Posts: 22
I think c) is the right answer because:
1)The integer myCount is static
This means that no matter how many instances of MyClass created, there is only ONE myCount, which belong to MyClass but NOT any of MyClass instance.
2)The method nextCount(), which increments myCount is synchronized and static.
Since the method is static, that means no matter how you call the method, such as MyClass.nextCount or new MyClass().nextCount. It is calling the method belong to the CLASS MyClass, not belong to any of its instance.
and the method is synchronized, that means the method and the variable in it will be locked when there is one thread accessing it. Other threads calling this method will only be notified when the method is done and available again. So, the integer myCount retrieved by any thread in that program should be unique.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
the static variable is declared private, so other classes can NOT modify it... this is the key problem in your understanding to it...

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
I think Ji's point is that some other class in the same package could modify the instance variable yourNumber without using the badly named method getYourNumber(). This looks like a very poorly designed mock exam question.
Happy trails
...full of sound and fury, signifying nothing --- William Shakespeare

kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
thanx guys.
Yeah,Ken ,I think that a thread want get myCount ,it must the same package class,because the method getYourNumber return void and variable yourNumber's modifier is ,I think the answer should be option D.
I agree. Here's the link:
subject: question about thread at J@Whiz1.4
jQuery in Action, 3rd edition