Sunteți pe pagina 1din 5

7/14/13

Need Explanation of unpredicted output even if methods are synchronized (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

Need Explanation of unpredicted output even if methods are synchronized


posted 12/30/2010 6:19:31 PM

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

Hi everyone, I have written a simple code fragment to learn threading concepts.I have attached code I have written.
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 . 1 4 . 1 5 .

p u b l i cc l a s sS h a r e d O b j e c t { p r i v a t ei n t c o u n t = 0 ; p u b l i c v o i d i n c r e m e n t C o u n t ( ) { s y n c h r o n i z e d ( t h i s ) { / * f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { t h i s . c o u n t + = 1 ; S y s t e m . o u t . p r i n t l n ( " I n c r e m e n t"+ c o u n t + "" +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 ( ) ) ; } * / t h i s . c o u n t + = 1 ; S y s t e m . o u t . p r i n t l n ( " I n c r e m e n t"+ c o u n t + "" +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 ( ) ) ; } } }

This is the shared object that contains the count variable among threads.This is my thread class
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 . p u b l i cc l a s sT h r e a d O n ei m p l e m e n t sR u n n a b l e { 0 2 . p r i v a t eS h a r e d O b j e c to b j ; 0 3 . p u b l i cT h r e a d O n e ( S h a r e d O b j e c to b j ) { 0 4 . t h i s . o b j = o b j ; 0 5 . } 0 6 . 0 7 . p u b l i cv o i dr u n ( ) { 0 8 . f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { 0 9 . o b j . i n c r e m e n t C o u n t ( ) ; 1 0 . } 1 1 . https://www.coderanch.com/t/521998/threads/java/Explanation-unpredicted-output-methods-synchronized

1/5

7/14/13
1 1 . 1 2 . 1 3 . 1 4 .

Need Explanation of unpredicted output even if methods are synchronized (Threads forum at JavaRanch)
} }

This is my Main class


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 . 1 4 . 1 5 .

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 ) { S h a r e d O b j e c to b j=n e wS h a r e d O b j e c t ( ) ; T h r e a d O n et h r e a d 1=n e wT h r e a d O n e ( o b j ) ; T h r e a d O n et h r e a d 2 =n e wT h r e a d O n e ( o b j ) ; 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 d 1 " ) ; 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 d 2 " ) ; t 1 . s t a r t ( ) ; t 2 . s t a r t ( ) ; S y s t e m . o u t . p r i n t l n ( " M a i nT h r e a di sC o m p l e t e d "+ 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 ( )) ; } }

So after executing this program.I mentioned the output I have got.Out put varies when I ran the program several times. C:\Documents and Settings\narambage\Desktop\HelloWOrld\Threads\Volatile>java Main Main Thread is Completed main Increment 1 thread1 Increment 2 thread1 Increment 3 thread1 Increment 4 thread1 Increment 5 thread1 Increment 6 thread1 Increment 7 thread1 Increment 8 thread1 Increment 9 thread2 Increment 10 thread2 Increment 11 thread2 Increment 12 thread2 Increment 13 thread2 Increment 14 thread2 Increment 15 thread2 Increment 16 thread1 Increment 17 thread1 Increment 18 thread1 Increment 19 thread1 Increment 20 thread1 Increment 21 thread1 Increment 22 thread1 Increment 23 thread1 Increment 24 thread1 Increment 25 thread2 Increment 26 thread2 Increment 27 thread2 Increment 28 thread2 Increment 29 thread2 Increment 30 thread2 Increment 31 thread2 Increment 32 thread2 Increment 33 thread2 https://www.coderanch.com/t/521998/threads/java/Explanation-unpredicted-output-methods-synchronized

2/5

7/14/13

Need Explanation of unpredicted output even if methods are synchronized (Threads forum at JavaRanch) Increment 33 thread2
Increment 34 thread1 Increment 35 thread1 Increment 36 thread1 Increment 37 thread2 Increment 38 thread2 Increment 39 thread2 Increment 40 thread2 Q: Why do I get a unpredictable out put even if I synchronized a incrementCount() method ?

Thinker Nuwan Arambage

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

posted 12/30/2010 6:27:56 PM

What is different in the output from what you expect?

6
Steve
I like...

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

posted 12/30/2010 11:24:39 PM

hi steve, I was expecting a this kind of output. C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\sync>java Main Main Thread is Completed main Increment 1 thread1 Increment 2 thread1 Increment 3 thread1 Increment 4 thread1 Increment 5 thread1 Increment 6 thread1 Increment 7 thread1 Increment 8 thread1 Increment 9 thread1 Increment 10 thread1 Increment 11 thread1 Increment 12 thread1 Increment 13 thread1 Increment 14 thread1 Increment 15 thread1 Increment 16 thread1 Increment 17 thread1 Increment 18 thread1 Increment 19 thread1 Increment 20 thread1 Increment 21 thread2 Increment 22 thread2 Increment 23 thread2 Increment 24 thread2 Increment 25 thread2 Increment 26 thread2 Increment 27 thread2 Increment 28 thread2 Increment 29 thread2

Increment 30 thread2 https://www.coderanch.com/t/521998/threads/java/Explanation-unpredicted-output-methods-synchronized

3/5

7/14/13

Need Explanation of unpredicted output even if methods are synchronized (Threads forum at JavaRanch) Increment 30 thread2 Increment 31 thread2 Increment 32 thread2
Increment 33 thread2 Increment 34 thread2 Increment 35 thread2 Increment Increment Increment Increment 36 37 38 39 thread2 thread2 thread2 thread2

Increment 40 thread2

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\sync> however I won't get it by executing previously mentioned code.when I change code as follows(changes to synchronized block & run method) I get the output what I need.

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 .

s y n c h r o n i z e d ( t h i s ) { f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { t h i s . c o u n t + = 1 ; S y s t e m . o u t . p r i n t l n ( " I n c r e m e n t"+ c o u n t + "" +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 ( ) ) ; } / * t h i s . c o u n t + = 1 ; S y s t e m . o u t . p r i n t l n ( " I n c r e m e n t"+ c o u n t + "" +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 ( ) ) ; * / }

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 .

p u b l i cv o i dr u n ( ) { / *f o r ( i n ti = 0 ; i < 2 0 ; i + + ) { o b j . i n c r e m e n t C o u n t ( ) ; } * / o b j . i n c r e m e n t C o u n t ( ) ; }

Question is this, Why wouldn't I get the expected output by executing previously mentioned code. Nuwan Arambage

Henry Wong author Sheriff Joined: Sep 28, 2004 Posts: 17009

posted 12/30/2010 11:39:41 PM

Nuwan A rambage wrote:

Question is this, Why wouldn't I get the expected output by executing previously mentioned code.

21
I like...

Let's reverse the question. Why do you expect to get the expected output by executing the first set of code? Why do you expect the whole loop to be atomic, when you grab (and release) the lock per each iteration of the loop?

Henry

https://www.coderanch.com/t/521998/threads/java/Explanation-unpredicted-output-methods-synchronized

4/5

7/14/13

Need Explanation of unpredicted output even if methods are synchronized (Threads forum at JavaRanch)

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)

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

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

Thanks Mr Henry, You learned me what I have missed "grab (and release) the lock per each iteration of the loop ".

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

subject: Need Explanation of unpredicted output even if methods are synchronized

Similar Threads Thread synchronization two simultaneous run commands on same class. locking?? about threads, using locks of the objects in synchronized blocks synchronized threads
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 09:05:24 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/521998/threads/java/Explanation-unpredicted-output-methods-synchronized

5/5

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