This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Object  locking in Multithreading. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Object  locking in Multithreading." Watch "Object  locking in Multithreading." New topic
Author

Object locking in Multithreading.

Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
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.


Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

Thanks for changing your display name according to the JavaRanch naming policy.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
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

Joined: Sep 28, 2004
Posts: 16811
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Jun 04, 2007
Posts: 71
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Object locking in Multithreading.
 
Similar Threads
Differene between == and equals() method
Multithreading
Deadlocks in Threads?
nested synchronized statements
synchronized methods