• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Synchronized with example

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic