• 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 access

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


here lets say a thread is accessing synchronized method() and a new childThread is started at line 1.
can some other thread access synchronized method while childThread is still running??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

here lets say a thread is accessing synchronized method() and a new childThread is started at line 1.
can some other thread access synchronized method while childThread is still running??




Newly created threads do not get the locks from their creating threads. In this pseudo code example, the lock is owned by the calling thread, so no thread, including the child thread, can get the lock, until the calling thread releases it.

Henry
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes correct but then while childThread is executing(say it is going to complete after 5 min) , can't its parent thread release the lock?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote: yes correct but then while childThread is executing(say it is going to complete after 5 min) , can't its parent thread release the lock?


Yes. The parent thread will release the lock as soon as it returns from method()
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

shashank dwivedi wrote: yes correct but then while childThread is executing(say it is going to complete after 5 min) , can't its parent thread release the lock?


Yes. The parent thread will release the lock as soon as it returns from method()



Also, why should the child thread be allowed to access the resources without the lock ? Just because the thread that created it owns the lock, doesn't mean that it owns the lock.

If the child thread want to access the resources, then it should acquire the lock, just like any other thread.

Henry

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To put it another way I assume he is asking the question; If you spawn a new thread from within this method, does that keep the method from returning until the thread that will spawned within it terminates thus holding the lock or can it return and release the lock even though the thread that was spawned within it is still running?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that was your question then from the following snapshot it can be seen that yes the method was able to return control to the calling method and the lock was released even though the thread was still in a runnable state which was spawned from within the method...
Screenshot-from-2014-04-29-17-43-37.png
[Thumbnail for Screenshot-from-2014-04-29-17-43-37.png]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rico Felix wrote:If that was your question then from the following snapshot it can be seen that yes the method was able to return control to the calling method and the lock was released even though the thread was still in a runnable state which was spawned from within the method...




Thanks for the investigation ... have a cow.

Having said that, keep in mind that the premise of parent / child threads, implied by the OP in the first post, doesn't exist. Yes, the newly created thread can inherit the priority, or the thread group, etc., but it isn't really a child thread. A thread is a thread is a thread.

When you create a thread, it is a new thread. And there isn't much (or any) relationship between the creating thread and the newly created thread. So, the newly created thread is just like any other thread, and will behave like any other thread.

Henry
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Having said that, keep in mind that the premise of parent / child threads, implied by the OP in the first post, doesn't exist.



This makes a lot of sense that you have mentioned it... Its not like a thread of execution is derived or inherited from another so there is no relationship between them...

I wish you had a new book on Threads containing the new concurrency APIs seeing that you are able to shed light on such a topic

 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rico and Henry for clearing my doubts..
 
Did Steve tell you that? Fuh - Steve. Just look at this 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