File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Synchronization- Where to put Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Synchronization- Where to put "synchronized"?" Watch "Thread Synchronization- Where to put "synchronized"?" New topic
Author

Thread Synchronization- Where to put "synchronized"?

Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
My code:


I want the method comm() to be accessed by only thread at a time. So I 'synchronized' the method comm(). There is only one object 'x' . But it not working. But when I synchronize the method run(), it works. Why is that so?
Thanks in advance.
Ajit
Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Ajit,

The reason why it's not working right now is because right now only the comm method is synchronized. That means that within the loop inside run(), the first thread that executes comm will acquire the lock, but as soon as comm returns the lock will be released, so the next thread could start running and call comm itself. To make synchronization work in this case you can either make run itself synchronized, or enclose the for loop in a synchronized block.


All code in my posts, unless a source is explicitly mentioned, is my own.
Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Ruben Soto wrote:Ajit,

The reason why it's not working right now is because right now only the comm method is synchronized. That means that within the loop inside run(), the first thread that executes comm will acquire the lock, but as soon as comm returns the lock will be released, so the next thread could start running and call comm itself. To make synchronization work in this case you can either make run itself synchronized, or enclose the for loop in a synchronized block.



Hi Ruben,

Making run itself synchronized works fine. But, i tried keeping the for loop in a synchronized block as follows:


But I still noticed both threads ONE and TWO entering the block at the same time. The output was as follows:


IN FOR: The current running thread is: ONE
IN FOR: The current running thread is: TWO
Current Thread name: TWO
IN FOR: The current running thread is: TWO
Current Thread name: TWO
IN FOR: The current running thread is: TWO
Current Thread name: TWO

Why does this happen when we have specified the loop to be synchronized..

Thanks


Fiona
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi,

You are getting that behavior because you are synchronizing on two different Thread objects. You would need to synchronize on a unique instance, like the unique MultiCommon object (which you can refer to by "this" in the run() method,) or the MultiUseMe instance referred to by x (do synchronized(this) or synchronized(x) and see if that works.)
Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
Q1: The following 3 options will work.(regardless of synchronized comm() ). But which object instance is locked?
Option1:public synchronized void run(){
This will lock the object test.
Option2:public void run(){
synchronized(x){
for (int i = 0; i<10; i++){
This will lock the object instance x.
Option3: public void run(){
synchronized(this){
for (int i = 0; i<10; i++){
This will lock the object test.
-----------------
Q2:If Thread.currentThread() returns current thread should this same as option3 above (using this)?
Q3:Ruben in your response "the MultiUseMe instance referred to by x (do synchronized(this)"-How can you get lock on object instance x by using 'do synchronized(this)'?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Synchronization- Where to put "synchronized"?