This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Understanding how synchronized works / possible bug?

1 reply
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Anyways beware of benchmarking in Java... it is very complicated to make reliable benchmarking in Java, I would suggest you to have a look to Google Caliper or to ask the guru of these ML. I am not an expert in that, but I wanted to warn you:))
Best Edmondo

2012/2/6 Sarah Mount <mount.sarah@gmail.com>
Thanks, and thanks to Edmondo too. I think my critical mistake was in
thinking of a synchronized block as a critical section of code, rather
than thinking of it as obtaining an exclusive lock at run time. Silly
me, but it's good to learn :)

Cheers,

Sarah

On Mon, Feb 6, 2012 at 13:44, Erik Osheim <erik@plastic-idolatry.com> wrote:
> On Mon, Feb 06, 2012 at 04:20:24AM -0800, snim2 wrote:
>> 2) Call synchronized as a method of "this" object, where "this"
>> extends Runnable. This doesn't work, some array elements do not get
>> updated:
>
> The problem here is that your workers aren't using same lock. Each
> worker is a different object (with a different lock associated with
> this) so obtaining that lock doesn't actually block anyone else.
>
> Whether you use a lock on the array or some other lock, it needs to be
> accessible by all threads.
>
> -- Erik



--
Sarah Mount, Senior Lecturer, University of Wolverhampton
website:  http://www.snim2.org/
twitter: @snim2

snim2
Joined: 2012-02-06,
User offline. Last seen 42 years 45 weeks ago.
Re: Understanding how synchronized works / possible bug?

Caliper looks interesting, thanks. There was a good paper on this
topic at OOPSLA a few years ago: "Statistically Rigorous Java
Performance Evaluation". I was more interested in correctness than
performance though, performance is something that is very much
dependant on the use case, in my view.

Sarah

On Mon, Feb 6, 2012 at 14:07, Edmondo Porcu wrote:
> Anyways beware of benchmarking in Java... it is very complicated to make
> reliable benchmarking in Java, I would suggest you to have a look to Google
> Caliper or to ask the guru of these ML. I am not an expert in that, but I
> wanted to warn you:))
>
> Best
> Edmondo
>
>
> 2012/2/6 Sarah Mount
>>
>> Thanks, and thanks to Edmondo too. I think my critical mistake was in
>> thinking of a synchronized block as a critical section of code, rather
>> than thinking of it as obtaining an exclusive lock at run time. Silly
>> me, but it's good to learn :)
>>
>> Cheers,
>>
>> Sarah
>>
>> On Mon, Feb 6, 2012 at 13:44, Erik Osheim
>> wrote:
>> > On Mon, Feb 06, 2012 at 04:20:24AM -0800, snim2 wrote:
>> >> 2) Call synchronized as a method of "this" object, where "this"
>> >> extends Runnable. This doesn't work, some array elements do not get
>> >> updated:
>> >
>> > The problem here is that your workers aren't using same lock. Each
>> > worker is a different object (with a different lock associated with
>> > this) so obtaining that lock doesn't actually block anyone else.
>> >
>> > Whether you use a lock on the array or some other lock, it needs to be
>> > accessible by all threads.
>> >
>> > -- Erik
>>
>>
>>
>> --
>> Sarah Mount, Senior Lecturer, University of Wolverhampton
>> website:  http://www.snim2.org/
>> twitter: @snim2
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland