• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Thread object lock

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: k&b(modified)


1. This code doesnt work as expected, it only prints Y's. and it looks like the other thread is started but didnt change the value of "name". using "obj" as lock in the above code works well.
2. Also using "this" as lock doesnt work(didnt synchronize)..
Any clues why?
Thanks
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this behavior is because of the name variable is static? I think when the threads started running that time the value of name is "Y".
Would you like to remove the static modifier from the variable and check the output again...
[ August 24, 2007: Message edited by: Al Mamun ]
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have marked it static in order to use it as lock, so that synchronization block can take effect. same when use "obj".
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I marked name as non-static and synchronized on 'this'. Now I get 200 X's followed by 200 Y's.
My question is:

Can we be sure to get 200 Z's followed by 200 Y's...?
I think the output is not predictable...please help me understand this.

Somewhat modified:


Since the two threads are working on different instances, they can both acquire the "this" lock for their respective objects.
I think we can get any output:?
ZZZZZ...YYYYY...
YYYYY....ZZZZZ...
ZYZYZY....
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THis code wont work ofcorse,becosue name is a static variable but you are using object level locks.

To lock static variables like name ,use class-level lock.

ie In synchnroinzed method or block
1. either use static keyword with synchronized in method definition
2. or in synchnronized block ,use ClassName.class ,to lock class,not object.

Hope it helps
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic