aspose file tools*
The moose likes Threads and Synchronization and the fly likes Synchronized block in run() method itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized block in run() method itself" Watch "Synchronized block in run() method itself" New topic
Author

Synchronized block in run() method itself

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi all,

I have written a sample code which has synchronized code block inside run() method.By looking at the output I wouldn't see any synchronization occurs.
Can somebody explain me what went wrong in my code.



Main Method



This is output what I have got once I execute the main method

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\Runnable>java Main

Thread Name : Thread 2 i 0
Thread Name : Thread 2 i 1
Thread Name : Thread 2 i 2
Thread Name : Thread 2 i 3
Thread Name : Thread 2 i 4
Thread Name : Thread 2 i 5
Thread Name : Thread 2 i 6
Thread Name : Thread 2 i 7
Thread Name : Thread 2 i 8
Thread Name : Thread 2 i 9
Thread Name : Thread 2 i 10
Thread Name : Thread 2 i 11
Thread Name : Thread 2 i 12
Thread Name : Thread 1 i 0
Thread Name : Thread 2 i 13
Thread Name : Thread 1 i 1
Thread Name : Thread 1 i 2
Thread Name : Thread 2 i 14
Thread Name : Thread 1 i 3
Thread Name : Thread 2 i 15
Thread Name : Thread 1 i 4
Thread Name : Thread 2 i 16
Thread Name : Thread 1 i 5
Thread Name : Thread 2 i 17
Thread Name : Thread 2 i 18
Thread Name : Thread 2 i 19
Thread Name : Thread 1 i 6
Thread Name : Thread 1 i 7
Thread Name : Thread 1 i 8
Thread Name : Thread 1 i 9
Thread Name : Thread 1 i 10
Thread Name : Thread 1 i 11
Thread Name : Thread 1 i 12
Thread Name : Thread 1 i 13
Thread Name : Thread 1 i 14
Thread Name : Thread 1 i 15
Thread Name : Thread 1 i 16
Thread Name : Thread 1 i 17
Thread Name : Thread 1 i 18
Thread Name : Thread 1 i 19

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\Runnable>

Thanks & Regards,

Nuwan Arambage


Thinker
Nuwan Arambage
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Nuwan Arambage wrote:


In this code, eacj new MyThread instance has its own obj Object. When you synchronize you get the lock on the obj Object that is local to just that one instance of MyThread. So when you have two instances there is no 'synchronization' because they are both gaining locks from different Objects. To synchronize two threads they need to synchronize on the same Object (the same instance).

Besides that, I am not sure the code you have there would show synchronization anyway. You are not protecting any shared data, and aren't performing any actions which would stand out as being synchronizing happening or not. Try to add some code that would make it easy to tell if there is some inter-leaving of processes. Something like this:

With this code, when you have synchronization you should always see an 'is about to do some work' statement immediately followed by an 'is done doing work' from the same Thread name. When synchronization is not working you should see at least some interleaving between the 'is about to do some work' statements from one thread and statements from the other thread.

-- Edit --
Steve Luke wrote:Besides that, I am not sure the code you have there would show synchronization anyway.

Okay, actually it will because the synchronization is on the outside of the for loop...


Steve
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi Steve,

I understood the mistake I have made in my code. I got your point
Thanks

Nuwan Arambage
rahul sab
Greenhorn

Joined: Nov 11, 2012
Posts: 1
Having MyThread.class instead of obj would have worked for you..

synchronized(MyThread.class)
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Hey rahul, isn't that post age old and poster more or less got his question answered? Welcome to ranch by the way.


Warm Regards,
Rakesh
 
 
subject: Synchronized block in run() method itself