• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Question about Thread.Need Help

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
The answer is C.
I am not clear about the question.Please explain in detail.Thank in advance!
:roll:
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Cheng:

Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
The answer is C.
I am not clear about the question.Please explain in detail.Thank in advance!
:roll:


Hi Andrew
Well the condition in the check method is X != Y. So if you use the answer B that means you have synchronized the threads and that both of the values are going to be equal. X!=Y is going to return false, while if you call setX & setY seperately you are not synchronizing the behavior so any thread can call any method with any value and the check X!=Y is going to return true, SO the answer C is right setX & setY called seperately will return true.
Hope this helps
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok let's see.
check will return true only if x and y are differnt .
in order for them to be different we need to put diffreent values in both of them using the functions given.
answer a is wrong as we will see later (cause it can return true).
answer b is wrong cause if the function setXY is called by multiple threads (with diffrents i all the time) it wont matter for the varaiables. this method is synchronized - meaning only 1 thread will be allowed in it at any given time so it will change the values of x and y together.
notice that the check mehtod is also synchronized - maining that only after 1 thread changed the values of both x and y to the same i ,
another thread will be able to check their status and see that they are equal thus false.
however in answer c is correct: because the setx and sety method are not synchronized different threads will be able to change them with different values simulatnatsly and check method can return true.
we already did what answer d suggested and allowed to change the x and y seperatly.
i know its messy but i hope u got it.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about D? I think it is also correct?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like i said:


we already did what answer d suggested and allowed to change the x and y seperatly .


d suggests we change the program to make x and y changes seperatly - which is a good idea BUT it is already possible in this program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic