• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Reetrant Synchroization

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I was reading Sun's tutorial about synchronization, where i came across a term Reetrant Synchroization.
It states that reetrant synchroization means that when a thread acquires the lock it is already acquiring more then once.
I thought of something like this let say for example



My question is, will the thread be able to acquire the lock again because it has already got the lock for the same object?
In such a situation the tutorial states, you have to take great care so that a thread doesn't block itself. Can a blocking
situation occur in this scenario?

 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes...a thread can again access the synchronized code if it is in the same lock. But, you have to make sure that it will not block itself.
http://java.sun.com/docs/books/tutorial/essential/concurrency/locksync.html
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thread executing the synchronized block in doStuff1() method will have acquired lock on "lock1". if the same thread,
executes the synchronized block in the doStuff2() method, it will try to re-acquire the lock it already has.
Since, locks are reentrant, the thread will be allowed to execute the code in the doStuff2 method.

For further reading see this: more flexible and scalable locking in JDK5.0
Note: you dont need the details in this link for the exam.
For the exam, you just have to know that if a thread is holding lock on an object it can reacquire it because of reentrancy.
 
I'm still in control here. LOOK at this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic