This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question doubt

 
Gaurang Rathod
Greenhorn
Posts: 22
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers.
This is a question from Whizlabs diagnostic test:


Options:
a) prints "ABC"
b) prints "123"

Answer given is "ABC" and rightly so.
Why is answer not "123"?
How could main thread change myRunnable object's instance variable s[] ?

Thanks advance.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array s is initialized by 1,2 and 3 when creating the instance MyRunnable class.
In line 18, you have started t1 (with MyRunnable). And in the next line current thread(Main thread) has been gone to the sleep mode for 4 seconds. It is more that enough to finish the t1 thread. Therefore 1,2,3 is printed.

s is changed after the t1 thread is terminated..!
 
Chintu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When jvm runs the code in the main method's synchronized block, even after starting the thread t1 which has myRunnable as target cannot run the thread as it needs lock on myRunnable to run the thread. And the lock is not available as it is alraedy taken by main method.
So main method's synchronized block will complete and set the entries in array as A, B and C.
And after the releaseing the lock from main method's synchronized block, it will print ABC as per run method of MyRunnable.

I hope this solves your doubt.
 
Chintu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep method does not release the lock on myRunnable.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurang Rathod wrote:Hello ranchers.
.......
Answer given is "ABC" and rightly so.
Why is answer not "123"?
How could main thread change myRunnable object's instance variable s[] ?

Thanks advance.


Why would you think it should be 123. What happens after you have invoked sleep in your main() method? Would the two threads interleave? What happens if you increase or decrease the sleep interval? Does that affect your output in anyway ( besides the time taken to execute the program)?

Chan.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.. Sorry.. Didn't have Chintu Ji's responses when I started with my response.

Thread like things into play here too!
 
Gaurang Rathod
Greenhorn
Posts: 22
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, got it now. Thanks for the replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic