This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on exercise about Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on exercise about Thread" Watch "Question on exercise about Thread" New topic

Question on exercise about Thread


Joined: Jan 23, 2003
Posts: 7
Do you know the answer

Under which conditions will check() return when called from a different class. Choose one

A. check() can never return true
B. check() can return true when set XY is called by multiple threads
C. check() can true when multiple threads call set X and set Y separately
D. check() can only return true if synchTest is changed to allow x and y to be set separately
[ January 25, 2003: Message edited by: Jessica Sant ]
hiti hitox

Joined: Jan 23, 2003
Posts: 3
A) isnt right, because of B)
Answer B) is right.
A call to setXY makes calls to the synchronized methods setX / setY.
But the method setXY isn't synchronized. So it can be called by multible threads.
When thread1 call setXY(8), method setX(8) called.
Here comes thread2 with call setXY(9).
Thread1 leaves setX(8) and call setY(8).
Now Thread2 can enter setX(9)
Thread1 call check() and ups! x==9, y==8!
C) Can't happen, cause setX/setY are private
D) completely beside it
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: Question on exercise about Thread
It's not a secret anymore!