This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized with example

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends, I am beginner in java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword. Then after that how the lock is released and how next thread access that.Please explain with example.Also explain object level lock and method level lock.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra Prakash wrote:Hi Friends, I am beginner in java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword.

When you have a synchronized method (or block), only one thread is allowed to execute that method (or block) at any given time.
Then after that how the lock is released and how next thread access that.

Lock is released whenever the thread is done with it (method in this case), so the JVM can assign another thread waiting for that lock.
Also explain object level lock and method level lock.

I haven't heard of "object level lock" (may be it's referring object lock ?) However there are two types of locks (monitors) in Java. Object lock & Class lock, those can be aquired by a thread. Class lock is per class basis & object lock is per object. If you have synchronized instance method(s) (or block), a thread wish to execute that method should aquire the lock for that particular object. Class lock is associated with the class. So any synchronized static method would need the class lock to be aquired by the thread which wish to execute that method. There are lot more to this but I suggest you go through Concurrency tutorial by Sun. And please CarefullyChooseOneForum when posting.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Vijitha pointed out, the servlet forum is not the correct place for this question. I'll move it to the appropriate forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic