Sunteți pe pagina 1din 3

7/14/13

Synchronized block in run() method itself [Solved] (Threads forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Threads and Synchronization

Author
Nuwan Arambage Ranch Hand Joined: May 05, 2010 Posts: 76

Synchronized block in run() method itself


posted 12/31/2010 12:33:18 AM

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

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 .

p u b l i cc l a s sM y T h r e a di m p l e m e n t sR u n n a b l e { O b j e c to b j = n e wO b j e c t ( ) ; p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d ( o b j ) { f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { S y s t e m . o u t . p r i n t l n ( " T h r e a dN a m e:" +T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( )+ "i"+ i ) ; } } } }

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 .

p u b l i cc l a s sM a i n { p u b l i cs t a t i cv o i dm a i n ( S t r i n g[ ] a r g s ) { M y T h r e a dt h r e a d 1 = n e wM y T h r e a d ( ) ; M y T h r e a dt h r e a d 2 = n e wM y T h r e a d ( ) ; T h r e a dt 1 = n e wT h r e a d ( t h r e a d 1 , " T h r e a d1 " ) ; T h r e a dt 2 = n e wT h r e a d ( t h r e a d 2 , " T h r e a d2 " ) ; t 1 . s t a r t ( ) ; t 2 . s t a r t ( ) ; } }

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

Synchronized block in run() method itself [Solved] (Threads forum at JavaRanch)


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 Thread Thread Thread Thread Name Name Name Name Name : Thread : Thread : Thread : Thread : Thread 1 1 1 1 1 i i i i i 15 16 17 18 19

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\Runnable> Thanks & Regards, Nuwan Arambage

Thinker Nuwan Arambage

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 12/31/2010 12:49:25 AM


Nuwan A rambage wrote:

view plain

c opy to c lipboard

print

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 .

p u b l i cc l a s sM y T h r e a di m p l e m e n t sR u n n a b l e { O b j e c to b j = n e wO b j e c t ( ) ; p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d ( o b j ) { f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { S y s t e m . o u t . p r i n t l n ( " T h r e a dN a m e:" +T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( )+ "i"+ i ) ; } } } }

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 .

p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d ( o b j ) { f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { S y s t e m . o u t . p r i n t l n ( " T h r e a dN a m e:" +T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( )+ "i sa b o u tt od os o m ew o r k . d o S o m e W o r k ( ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a dN a m e:"+T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( )+"i sd o n ed o i n gw o r k . " ) ; } } } p r i v a t ev o i dd o S o m e W o r k ( ){ t r y{

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 ! } }

Synchronized block in run() method itself [Solved] (Threads forum at JavaRanch)

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

posted 12/31/2010 9:15:46 AM

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

posted 11/11/2012 2:50:54 PM

Having MyThread.class instead of obj would have worked for you.. synchronized(MyThread.class)

Rakesh K. Cherukuri Ranch Hand Joined: Jun 01, 2010 Posts: 47


I like...

posted 11/11/2012 3:33:29 PM

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

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Synchronized block in run() method itself

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/522029/threads/java/Synchronized-block-run-method

3/3

S-ar putea să vă placă și