aspose file tools*
The moose likes Threads and Synchronization and the fly likes Some Thread Synchornization questions ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Some Thread Synchornization questions ?" Watch "Some Thread Synchornization questions ?" New topic
Author

Some Thread Synchornization questions ?

Java Kt
Greenhorn

Joined: Jun 10, 2011
Posts: 6

i have couple of doubts regarding synchronization in java threads


1)When we put synchronize before any method that makes sense only if the same object conatining the synchronized method
is shared among different threads. Lets say we have a class class1 containg synchronized method1. Now if two threads(say t1
and t2) sharing the same insatnce of class1(say object1) then only synchronized keyword will come into picture .
But not ,if we have t1 and t2 thread accessing the same synchronized method 1 on different instances of class1


2) when a thread t1 enters a synchronize method (on object o1)then it gets the lock on complete object. then even if thread t2 want to access
any other method of the same object o1 either it is synchronized or not then it can not access

3)How the locking happens in case of static synschronized methods

4)Say a thread t1 called the wait method on method1 of object1. Then if another thread t2 callled the notify on method2 of sameobject. Does it make the thread
t1 runnable.

5)one more question reagaring 4 if 10 threads are waiting on object then after calling notify which thread out 10 will be runnable? Will it any random
thread?
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi,
1) this is true. For this we need to understand why we synchronize. Please note that synchronization is required only when 2 or more threads are accessing/modifying same data. Same data here means same java object. Thus if there are different threads working on different data then threads would not conflict with each other and thus there is no need to synchronize.
That is why java allows threads working on different objects to enter the same synchronized method at same time.

2) This is true. The reason is java's thread processing assumes that we are working with encapsulated data. ie each object represents some data which can be accessed through its methods only.
Thus if one thread(t1) is working with a object then another thread(t2) should be allowed to access the same object as it may lead to modification of underlying data(read object) which is definitely not warranted.
I know people can say that one method touches one specific attribute and another method touches a different attribute of same object. Thus each method is independent. I would say that a language is designed to take care of worst scenario.
4) Yes
5) the thread which gets to run depends on various factor (like thread priority, how OS implements multi-tasking) . You can never be sure about which of the waiting thread will run.
This is not as bad as it sounds as most OS handle multitasking in an effective manner allowing every task to be given a fair chance of execution.
Hope it helps.

Thanks,
Satish
Java Kt
Greenhorn

Joined: Jun 10, 2011
Posts: 6
Thanks Satish for detailed reply. But i am still wondering about two points i.e

2) when a thread t1 enters a synchronize method (on object o1)then it gets the lock on complete object. then even if thread t2 want to access
any other method of the same object o1 either it is synchronized or not then it can not access

in nutshell you said if the thread t1 has got lock of object o1 then thread t2 can not even execute the methods of o1 which are not synchronized.

but if that is true what will happen if thread t2 already executing method2 of o1 which is not synchronized. and at the same time thread t1 want to execute
synnchronized method of o1 . will it allow?


3)How the locking happens in case of static synschronized methods

i think in this case thread gets the lock on class object associated with class not on object?
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi,
1) No this is not correct. A thread(say t1) can't enter any synchronized method of a object if any other threads(say t2) already owns monitor of that object. t1 can still execute any non synchronized method of that object.
Pleasev understand the reasoning behind this. Initially I had hard time understanding all this because I was trying to understand it from theory perspective. It will be easier for you to understand the same if you can apply this to a practical problem.

Thanks,
Satish
Philip Grove
Ranch Hand

Joined: Aug 18, 2009
Posts: 68

Coming from a theory perspective free of programming languages is, in my opinion, often the easiest way to approach the issue of thread synchronization. BTW, a monitor is an object designed to be used safely by multiple threads.

As soon as you declare a method in a class synchronized the whole class becomes a monitor but only related to that method, so use of the word monitor for anything other than a class where all method are declared synchronized is discouraged.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some Thread Synchornization questions ?