• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object locking in Multithreading.  RSS feed

 
Saurabh Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me to answer below question related to object locks in Multithreading.

Consider the example in a class where there are two methods.
One synchronized method and other Unsynchronized i.e S1 and S2.
I am creating two threads using same object of class i.e T1 and T2.
My question is:-
When thread T1 is accesing synchronized method S1, it will have a lock on object.
At the same time can T2 access the unsynchronized method S2.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a question, it's a statement. What is your question?

Also, your display name is obviously fictitious. That is not allowed on JavaRanch. The moderator of this forum will shortly pick you up on it and insist you change it, but you could save their time by changing it now.
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
yes, T2 can access unsynchronized method.as you told that T1 is accessing Sysncronized method, So there is no problem to access Non-Syncronized method at all.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for changing your display name according to the JavaRanch naming policy.
 
Saurabh Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, for the suggestion.
The question is: can we access any other unsynchronized method of a class when a thread is accessing a synchronized method.And remember both threads are created using same object of class.

Shankar
can you please explain me when the object is locked by T1, howcome T2 can access any method of same object.This fails complete concept of object locking.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Gupta:
Shankar
can you please explain me when the object is locked by T1, howcome T2 can access any method of same object.This fails complete concept of object locking.


By whose concept?

Anyway, synchronization is cooperative. When a thread is synchronized on a lock, it is assumed that all threads that compete with that thread is also synchronized on that lock. If the other threads are calling a method that is *not* synchronized, Java assumes that the programmer knows what he/she is doing and allow it.

Henry
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Gupta:
This fails complete concept of object locking.


I think you have misunderstood the concept of a lock in Java. The name "lock" has lead you to assume it automatically controls access to the fields and methods of the object with which it is associated. It does not do so. It only interacts with synchronised blocks and methods, and with the wait() and notify[All]() methods. You need to use these explicitly to code whatever concurrent access controls are appropriate to your class.
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry Wong ,

Locking concept comes ,when Syncronizartion is there.when we want to access a syncronized method, we should get the lock before trying to access. For non-synchronized methods lock is not required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!