Documente Academic
Documente Profesional
Documente Cultură
Java FAQ
Recent Topics
Register / Login
Author
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.
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .
Main Method
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 .
This is output what I have got once I execute the main method C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\Runnable>java Main Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Name Name Name Name Name Name Name Name Name Name Name : Thread : Thread : Thread : Thread : Thread : Thread : Thread : Thread : Thread : Thread : Thread 2 2 2 2 2 2 2 2 2 2 2 i i i i i i i i i i i 0 1 2 3 4 5 6 7 8 9 10
https://www.coderanch.com/t/522029/threads/java/Synchronized-block-run-method
1/3
7/14/13
view plain
c opy to c lipboard
6
I like...
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .
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:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . " ) ; 0 5 . 0 6 .
https://www.coderanch.com/t/522029/threads/java/Synchronized-block-run-method
0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 .
2/3
7/14/13
1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 .
p r i v a t ev o i dd o S o m e W o r k ( ){ t r y{ T h r e a d . s l e e p ( 1 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ni e ){ / / D o n ' tc a r e . p . s .d o n ' ti g n o r ee x c e p t i o n sl i k et h i si nr e a lc o d e ! } }
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
Having MyThread.class instead of obj would have worked for you.. synchronized(MyThread.class)
Hey rahul, isn't that post age old and poster more or less got his question answered? Welcome to ranch by the way.
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads Thread java.lang.IllegalMonitorStateException: Iam new to threads locking?? about threads, using locks of the objects in synchronized blocks Threading issue Need Explanation of unpredicted output even if methods are synchronized
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 09:03:46 .
https://www.coderanch.com/t/522029/threads/java/Synchronized-block-run-method
3/3