Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15427

Re: unchecked conversion warning.

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: unchecked conversion warning.
Date 2012-06-19 23:14 +0200
Message-ID <a4c8d9Fsh9U1@mid.individual.net> (permalink)
References <3s7cs7hd18l0ffci55ns0286n4lc4cutlu@4ax.com> <265ea956-a3ed-4e76-8fc9-9ce9728f6b37@googlegroups.com> <vOmdnf2Cqs6MBEjSnZ2dnUVZ_tSdnZ2d@earthlink.com> <a43fb8Fj1vU1@mid.individual.net> <1bf8c686-8f7f-4ff1-94ff-5ea1a1b1fecd@googlegroups.com>

Show all headers | View raw


On 18.06.2012 21:28, Lew wrote:
> Robert Klemme wrote:
>> I totally agree.  Btw, there is one interesting detail: JavaDoc of
>> Collections.synchronizedList() and the other synchronized*() methods
>> explicitly state that the mutex used for synchronizing is that of the
>> returned object:
>>
>> http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29
>
> I see no such promise there.

<quote>
It is imperative that the user manually synchronize on the returned list 
when iterating over it:

   List list = Collections.synchronizedList(new ArrayList());
       ...
   synchronized (list) {
       Iterator i = list.iterator(); // Must be in synchronized block
       while (i.hasNext())
           foo(i.next());
   }


Failure to follow this advice may result in non-deterministic behavior.
</quote>

Pardon, my wording was wrong: it's not explicitly stated but it follows 
immediately from the quote.  For me that was "explicit" enough. ;-)

> And in fact it's not true. The source for the 'synchronizedCollection()' family
> of methods reveals that there's a separate field 'mutex' of type 'Object' on
> which the methods synchronize.

Did you see how it's initialized?  It's initialized with this, i.e. the 
wrapper instance.  The field serves the sole purpose to be able to 
synchronize on a parent collection when a dependent collection is 
created (e.g. in 
java.util.Collections.SynchronizedRandomAccessList.subList(int, int)). 
So normally the monitor of the wrapper is used as illustrated by the 
JavaDoc quoted.

>> No such statement is done about Vector.  So while the source code of
>> Vector tells us that the Vector instance is used to synchronize the
>> JavaDoc does not give any guarantee about that and - at least
>> theoretically - Sun/Oracle could change that and synchronize on another
>> instance.
>
> And that would rarely matter, unless a person were adding actions that tried to
> synchronize on the same monitor.

No, it also matters in cases like the example quoted above, i.e. when 
you need to perform multiple operations on the Vector instance: when the 
monitor which the Vector instance uses internally is different from the 
one used for the external synchronization then you do not have thread 
safety.  Depending on the logic anything from inconsistent contents to 
CME can happen.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

unchecked conversion warning. Jens - 2012-05-30 15:32 +0200
  Re: unchecked conversion warning. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-05-30 13:48 +0000
    Re: unchecked conversion warning. Jens - 2012-05-30 15:52 +0200
  Re: unchecked conversion warning. Robert Klemme <shortcutter@googlemail.com> - 2012-05-30 07:54 -0700
    Re: unchecked conversion warning. Jens - 2012-05-31 22:23 +0200
      Re: unchecked conversion warning. Lew <lewbloch@gmail.com> - 2012-05-31 13:40 -0700
      Re: unchecked conversion warning. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-31 16:50 -0400
        Re: unchecked conversion warning. Lew <lewbloch@gmail.com> - 2012-05-31 17:18 -0700
        Re: unchecked conversion warning. Robert Klemme <shortcutter@googlemail.com> - 2012-06-01 08:14 +0200
          Re: unchecked conversion warning. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-01 08:48 -0400
            Re: unchecked conversion warning. Robert Klemme <shortcutter@googlemail.com> - 2012-06-01 07:32 -0700
            Re: unchecked conversion warning. Roedy Green <see_website@mindprod.com.invalid> - 2012-06-10 12:16 -0700
              Re: unchecked conversion warning. Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-10 22:08 +0000
                Re: unchecked conversion warning. Lew <noone@lewscanon.com> - 2012-06-10 17:09 -0700
                Re: unchecked conversion warning. Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-11 01:31 +0000
        Re: unchecked conversion warning. Jim Janney <jjanney@shell.xmission.com> - 2012-06-01 08:41 -0600
          Re: unchecked conversion warning. Patricia Shanahan <pats@acm.org> - 2012-06-01 08:44 -0700
            Re: unchecked conversion warning. markspace <-@.> - 2012-06-01 09:08 -0700
            Re: unchecked conversion warning. Gene Wirchenko <genew@ocis.net> - 2012-06-01 09:31 -0700
            Re: unchecked conversion warning. Jim Janney <jjanney@shell.xmission.com> - 2012-06-01 11:02 -0600
            Re: unchecked conversion warning. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-01 13:54 -0400
              Re: unchecked conversion warning. Patricia Shanahan <pats@acm.org> - 2012-06-01 16:44 -0700
                Re: unchecked conversion warning. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-01 20:24 -0400
                Re: unchecked conversion warning. Patricia Shanahan <pats@acm.org> - 2012-06-01 19:36 -0700
          Re: natural/native language capabilities (was: unchecked conversion warning.) Jim Janney <jjanney@shell.xmission.com> - 2012-06-01 11:01 -0600
        Re: unchecked conversion warning. Roedy Green <see_website@mindprod.com.invalid> - 2012-06-10 12:13 -0700
        Re: unchecked conversion warning. Arne Vajhøj <arne@vajhoej.dk> - 2012-06-20 19:28 -0400
  Re: unchecked conversion warning. Broad Liyn <broadliyn@gmail.com> - 2012-06-10 21:52 -0700
    Re: unchecked conversion warning. Lew <noone@lewscanon.com> - 2012-06-10 23:50 -0700
    Re: unchecked conversion warning. Patricia Shanahan <pats@acm.org> - 2012-06-11 00:06 -0700
      Re: unchecked conversion warning. Robert Klemme <shortcutter@googlemail.com> - 2012-06-16 15:17 +0200
        Re: unchecked conversion warning. Lew <lewbloch@gmail.com> - 2012-06-18 12:28 -0700
          Re: unchecked conversion warning. Robert Klemme <shortcutter@googlemail.com> - 2012-06-19 23:14 +0200
            Re: unchecked conversion warning. Lew <lewbloch@gmail.com> - 2012-06-19 17:15 -0700
      Re: unchecked conversion warning. Arne Vajhøj <arne@vajhoej.dk> - 2012-06-20 19:22 -0400
    Re: unchecked conversion warning. Arne Vajhøj <arne@vajhoej.dk> - 2012-06-20 19:20 -0400

csiph-web