• 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

Synchronization

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If in a class two synchronized method are defined.Two threads are created of the class. If thread1 has acquired a lock on method1 and at the same time thread2 is trying to access the method2 then will it be able to acquire lock on the method2 or thread2 has to wait until thread1 releases the lock on method1.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The answers depend on what you mean exactly by "Two threads are created of the class". If the two threads share a single object of the class, then yes, one has to wait for the other. But if there are two instances of this class, and each thread has its own instance (or is its own instance, if you meant that the class was a subclass of Thread) then they will not interfere, and each will run independently.
 
Rohit Soni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply.It cleared my doubt.
I need one more help from you.
Can you tell by giving one example by creating class and synchronized method and thread trying to access those synchronized method, then it will be more easy to understand.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See "Synchronizing Threads" in Java tutorial http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic