Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on exercise about Thread

 
brian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ex:
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
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brian,
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic