• 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

Thread question doubt

 
Greenhorn
Posts: 22
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sleep method does not release the lock on myRunnable.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, got it now. Thanks for the replies.
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic