• 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

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: 4116
72
Mac TypeScript Chrome Java Linux
  • 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: 9707
43
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...
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like 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