Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization  RSS feed

 
Rohit Soni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Peter Curila
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See "Synchronizing Threads" in Java tutorial http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!