File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized block in run() method itself

 
Nuwan Arambage
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nuwan Arambage
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

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

Nuwan Arambage
 
rahul sab
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having MyThread.class instead of obj would have worked for you..

synchronized(MyThread.class)
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey rahul, isn't that post age old and poster more or less got his question answered? Welcome to ranch by the way.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic