Beruflich Dokumente
Kultur Dokumente
Java FAQ
Recent Topics
Register / Login
Author
Synchronous objects.
posted 5/21/2007 1:20 PM
Following is the code i have written, to check that what will happen if both the threads try to access different method of a same object, which is synchronized, but the threads are of different priorities(I kept on changing the priorities to get different values). My expectation was due to the huge different in priority there should be the output will be asynchronous (I mean sometimes there will be high number of got statements, and sometimes the opposite)
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 .
/ /T h i si su s e dt oc a l c u l a t et h ei n t e rc o m m u n i c a t i o n . . . / /T h i si su s e dt oc a l c u l a t et h ei n t e rc o m m u n i c a t i o n . . . c l a s sf a c t o r y { i n tl i _ v a l u e ; b o o l e a nl b=t r u e ; p u b l i cv o i dg e t ( ) { t r y {
1/8
www.coderanch.com/t/233805/threads/java/Synchronous-objects
7/14/13
1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 .
S y s t e m . o u t . p r i n t l n ( " Ih a v eg o tt h ev a l u e"+l i _ v a l u e ) ; T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { / / } } p u b l i cv o i dp u t ( i n tl i ) { i n tl i _ v a l u e=l i ; t r y { S y s t e m . o u t . p r i n t l n ( " Ih a v ep u tt h ev a l u e"+l i _ v a l u e ) ; T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { / / } } } c l a s sp r o d u c e ri m p l e m e n t sR u n n a b l e { T h r e a dt p r o d ; i n tl i _ r e s ; f a c t o r yf p u t ; p r o d u c e r ( f a c t o r yf ) { / / c r e a t eT h r e a dp r o d u c e r t p r o d=n e wT h r e a d ( t h i s , " p r o d " ) ; f p u t=f ; l i _ r e s=0 ; } p u b l i cv o i dr u n ( ) { b o o l e a nl b=t r u e ; s y n c h r o n i z e d ( f p u t ) { t r y { i n tl i _ i=0 ; w h i l e( l b ) { f p u t . p u t ( l i _ i+ + ) ; } } c a t c h ( E x c e p t i o ne ) { / / } } } } c l a s sc o n s u m e ri m p l e m e n t sR u n n a b l e { T h r e a dt c o n ; f a c t o r yf g e t ;
2/8
www.coderanch.com/t/233805/threads/java/Synchronous-objects
7/14/13
7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 .
but instead the result was bit odd!! The outcome was either the only the statment - "I have put the value..". or sometimes only the statement - "I have got the value..". I understand that the result will be asynchronous and need to get it synchonized through wait();/notifyall(); , but why such an asynchronous result! Can anyone throw some light on this? I am strugling for a long time to solve it. But unable to do so. Thanks in advance. Babai.
www.coderanch.com/t/233805/threads/java/Synchronous-objects 3/8
7/14/13
[ May 21, 2007: Message edited by: babai bhaumik ] [ added code tags - Jim ] [ May 21, 2007: Message edited by: Jim Yingst ]
Be bold.
Anupam Sinha Ranch Hand Joined: Apr 13, 2003 Posts: 1088
You are holding a lock on a single factory object. One who gets the lock never relieves the lock. Hence the output.
Hi All, As per above suggestion, and then looking to some other sources I have changed my code, as below. The result is working fine. But what approach can I take when, there are multiple threads accessing same object? Boolean will not work. Do we use string or char in this case? Please suggest.
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 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 .
www.coderanch.com/t/233805/threads/java/Synchronous-objects
7/14/13
3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 .
} p u b l i cv o i dp u t ( i n tl i ) { t r y { i f( l b ) { w a i t ( ) ; } } c a t c h ( E x c e p t i o ne ) { / / } t r y { l i _ v a l u e=l i ; S y s t e m . o u t . p r i n t l n ( " Ih a v ep u tt h ev a l u e"+l i _ v a l u e ) ; T h r e a d . s l e e p ( 3 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { / / } l b=t r u e ; n o t i f y ( ) ; } } c l a s sp r o d u c e ri m p l e m e n t sR u n n a b l e { T h r e a dt p r o d ; i n tl i _ r e s ; f a c t o r yf p u t ; p r o d u c e r ( f a c t o r yf ) { / / c r e a t eT h r e a dp r o d u c e r t p r o d=n e wT h r e a d ( t h i s , " p r o d " ) ; f p u t=f ; l i _ r e s=0 ; } p u b l i cv o i dr u n ( ) { b o o l e a nl b=t r u e ; s y n c h r o n i z e d ( f p u t ) { t r y { i n tl i _ i=0 ; w h i l e( l b ) { f p u t . p u t ( l i _ i+ + ) ; } } c a t c h ( E x c e p t i o ne ) { / / } } }
5/8
www.coderanch.com/t/233805/threads/java/Synchronous-objects
7/14/13
9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 . 1 4 7 . 1 4 8 .
Thanks in advance. Babai. [ added code tags - Jim ] [ May 21, 2007: Message edited by: Jim Yingst ]
Anupam Sinha
www.coderanch.com/t/233805/threads/java/Synchronous-objects 6/8
7/14/13
Sorry but I am unable to understand your question. Can you rephrase it. Yeah also use code tag to post some code. [ May 21, 2007: Message edited by: Anupam Sinha ]
In the above example I have used one boolean variable and depending upon the boolean value I am deciding which thread should go for wait(). But say there are multiple thread accessing same object. Now how can I give decisions in my code to decide that which thread should go for wait(). Tag : Do you mean to say that I should use tag for methods functionality? If anything else please update. Thanks in advance. Babai
Jim Yingst Wanderer Sheriff Joined: Jan 30, 2000 Posts: 18670
Anupam is suggesting that you should use code tags to make you code more readable (in particular, preserving indentation). I've already added them to your previous two posts above. As for your question, it seems like you can use any mechanism you want. I really can't tell what the goal is, other than exloring to see what happens - and that's up to you to decide what you want to explore. If you need more states than a boolean allows, an int certainly sounds like a reasonable alternative. [ May 21, 2007: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
7/14/13
threading access
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:39:38 .
www.coderanch.com/t/233805/threads/java/Synchronous-objects
8/8