• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads problem  RSS feed

 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



1.) At #1 the value of x is guarenteed to be 11

2.) At #2 the value of x is guarenteed to be 12

3.) If a Thread.sleep(500) is added just before #1, x is guarenteed to be 11

4.) If a Thread.sleep(500) is added just before #2, x is guarenteed to be 12

5.) The output of this program cannot be guarenteed

6.) The runnable implementation is an anonymous inner class


Answer: (5),(6)

the above question is from Certpal Mock test 1.6 part 2

But My answer is (3),(4),(6)

who is correct??n why?




Also please suggest me some realistic free mock test conducting websites...

thnks in advance


with regards
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think sleeping half a second will guarantee anything in this case?
The other thread might take five seconds to start to run.
On any decent computer the output will be what you think.
But what is important in this case is that it is not guaranteed by any means!
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pawel
In the above program if before #1
if Thread.sleep(600) is added then main thread
goes to sleep for 600ms for sure so only other user-defined
thread
sitting in the runnable pool should definitely
get executed and much before 600 ms it should
complete and die..


Therefore Output is guaranteed.....(3) is correct
and in similar fashion (5) is also correct

The other thread might take five seconds to start to run.


But what I know is,thread started means it goes to runnable pool
and here if main thread goes to sleep the only other thread in runnable pool
is bound to run..........

please tell me where my understnding has gone wrong???

thnks in advance


 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopam Samal wrote:so only other user-defined thread sitting in the runnable pool should definitely get executed and much before 600 ms it should complete and die...

Yeah. It should. As I said, on any decent computer that will happen, but nothing in Java guarantees that.
Roopam Samal wrote:But what I know is,thread started means it goes to runnable pool and here if main thread goes to sleep the only other thread in runnable pool is bound to run..........

Yes, it is bound to run. But it might take 5 seconds to "realize" that it should run. And withind those 5 seconds the other thread's sleep period will end and that other thread would be first to run.
As I said. It is almost sure that the output will be what you think. And again, nothing guarantees that.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopam Samal wrote:...only other user-defined
thread sitting in the runnable pool should definitely
get executed...

Therefore Output is guaranteed...

...But what I know is,thread started means it goes to runnable pool
and here if main thread goes to sleep the only other thread in runnable pool
is bound to run...

You are making a lot of assumptions and guesses. Just because there is only one other user thread, doesn't mean there aren't other background threads to run, and even if there aren't other background threads, there can be other processes, and threads running on the OS. And the OS has final say over what threads get run and when anyway - so even if there is only your two threads on the entire OS (very unlikely) there is no guarantee that the one you want to run will actually run. All of that aside, even if we assume the thread runs when you expect it to, and the value gets set, there is no guarantee that the value set by the thread is visible to the main thread - there are very limited and specific situations where this guarantee is made, and the posted code doesn't meet any of them. So maybe the main thread sleeps, and the second thread runs, and the value is set in the second thread, then the main thread finishes, but the second thread didn't publish the value, so the main thread doesn't see it. You have done nothing to guarantee that it will.

See JLS 17.4: The Java Memory Model for all the details about what guarantees can and cannot be made.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!