[concurrency-interest] ConcurrentHashMap NullPointerException

Tim Peierls tim at peierls.net
Fri Jan 4 23:36:26 EST 2008


Thread 2 might see either 0 or 1. Although the submission of the first
Runnable *happens-before* the call to changeVal, and the submission of the
second Runnable *happens-before* the call to readVal, you cannot conclude
that the call to changeVal *happens-before* either the submission of the
second Runnable or the call to readVal.

If the field were volatile, or if both methods of X were synchronized, then
the write to the field *would* *happen-before* the read of the field.

The javadoc snippet that you quoted implies that the write to the field
would *happen-before* the read of the field if the sequence was:

t1: in Thread-X, call changeVal(1)  -- write to field
t2: submit Runnable to thread pool from Thread-X
t3: in Thread-1 (of thread pool), Runnable.run calls readVal  -- read of
field

--tim

On Jan 4, 2008 10:48 PM, BJ Low <notorand at gmail.com> wrote:

> Hi,
>
> I am pretty sure of below. It is very hard for me to post the code cause
> the code is proprietary, but perhaps let me ask this in another way
>
> Suppose I have a class below
>
> class X {
>      int i = 0;
>
>      void changeVal(int i) {
>         this.i = i;
>      }
>
>      int readVal() {
>         return this.i;
>      }
> }
>
> At time t1, Thread-X submits a Runnable to threadpool,
> At time t2, Thread-1 from the threadpool runs the Runnable and call
> changeVal(1);
> At time t3, Thread-X submits another Runnable to threadpool
> At time t4, Thread-2 from the threadpool runs the Runnable and call
> readVal();
>
> The question is, will Thread-2 see the value of i as 0 or 1? Cause I am
> not quite sure of this statement from the javadoc
>
> "Actions in a thread prior to the submission of a Runnable to an Executor
> *happen-before* its execution begins. Similarly for Callables submitted to
> an ExecutorService"
>
>
> On Jan 4, 2008 3:27 PM, David Holmes <dcholmes at optusnet.com.au > wrote:
>
> >  BJ,
> >
> > It isn't clear what the exact sequence of events is in the two threads,
> > nor where this locking on SS is applied.
> >
> > Is it certain that Y is not null when you try to store <x, y> into the
> > map? (Just checking.)
> >
> > Is it certain that Y is in the map before the key X is made available to
> > thread-2?
> >
> > It's very hard to debug this via thought-experiment. :)
> >
> > Cheers,
> > David Holmes
> >
> > -----Original Message-----
> > *From:* BJ Low [mailto:notorand at gmail.com]
> > *Sent:* Friday, 4 January 2008 5:22 PM
> > *To:* dholmes at ieee.org
> > *Cc:* concurrency-interest at cs.oswego.edu
> > *Subject:* Re: [concurrency-interest] ConcurrentHashMap
> > NullPointerException
> >
> > According to this
> > http://java.sun.com/j2se/1.5.0/docs/guide/vm/server-class.html, since my
> > server is a quad-core processsor with 4g ram, it is running in server mode
> > then.
> >
> > At time t1, Thread-1 will create the key X, put the value Y to CHM, and
> > store the key to another object Z
> > At time t2, Thread-2 will then take out the key X from object Z, get the
> > value Y from CHM using key X, where t2 > t1
> >
> > The storing of key X to object Z is during object Z constructor method.
> > the retrieval of key X is a simple getter() method without any
> > synchronization or volatile.
> >
> > However, i notice another additional thing after inspecting the code
> > again, that is before Thread-1 enter the method to create the key X etc, it
> > is synchronized around an object SS, same thing for Thread-2. Therefore, it
> > cannot be the case of Java Memory Model interfering. If it is not JMM, then
> > what could it be that causes ConcurrentHashMap to return null?
> >
> > On Jan 4, 2008 2:46 PM, David Holmes <dcholmes at optusnet.com.au> wrote:
> > > How is the key published from Thread-1 to Thread-2?
> > >
> > > Java Memory Model issues tend to manifest through agressive
> > optimizations in
> > > the VM rather than actual memory consistency problems. Are you running
> > the
> > > server VM or client?
> > >
> > > Cheers,
> > > David Holmes
> > >
> > > > -----Original Message-----
> > >
> > > > From: BJ Low [mailto: notorand at gmail.com]
> > > > Sent: Friday, 4 January 2008 4:41 PM
> > > > To: dholmes at ieee.org
> > > > Cc: concurrency-interest at cs.oswego.edu
> > > > Subject: Re: [concurrency-interest] ConcurrentHashMap
> > > > NullPointerException
> > > >
> > > >
> > > > Hi,
> > > >
> > > > What I have implemented is an NIO wrapper that is similar to Apache
> > > > MINA. In this, there are multiple n threads that is handling all the
> > m
> > > > sockets reads/writes. However, it is guaranteed that only 1 thread
> > can
> > > > notify the listener of each connections IO events. For example
> > > > Thread-1 notify listener onNewConnection(), and Thread-2 notify
> > > > listener onRead(), however Thread-1 and Thread-2 can never notify
> > > > concurrently, multithreaded underneath, but appears monothreaded
> > > >
> > > > Right now, I am suspecting that it might be the case that Thread-1
> > > > when onNewConnection() constructs the key X and stores Y into the
> > > > ConcurrentHashMap, Thread-2 fails to see the key as initialized due
> > to
> > > > Java Memory Model(even though Thread-2 executes after Thread-1),
> > > > resulting in null that is passed to CHM.get() and therefore
> > resulting
> > > > in null return value
> > > >
> > > > On Jan 4, 2008 1:58 PM, David Holmes < dcholmes at optusnet.com.au>
> > wrote:
> > > > > BJ,
> > > > >
> > > > > Can you provide further information: code sketch, exception stack
> > trace
> > > > > details. I'm unclear whether get() is returning null
> > > > unexpectedly or whether
> > > > > get() is throwing the NPE. The usual source for the latter is
> > > > misbehaving
> > > > > equals() methods.
> > > > >
> > > > > Cheers,
> > > > > David Holmes
> > > > >
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: concurrency-interest-bounces at cs.oswego.edu
> > > > > > [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of
> > BJ Low
> > > > > > Sent: Friday, 4 January 2008 3:08 PM
> > > > > > To: concurrency-interest at cs.oswego.edu
> > > > > > Subject: [concurrency-interest] ConcurrentHashMap
> > NullPointerException
> > > > > >
> > > > > >
> > > > > > Hi all,
> > > > > >
> > > > > > I am seeing some bizarre behavior in the ConcurrentHashMap where
> > one
> > > > > > thread constructs an objects and puts it into a the map and
> > another
> > > > > > thread gets the object and sees it as null. Has any one
> > encountered
> > > > > > this problem before. I believe this problem is posted sometime
> > ago by
> > > > > > Eric Zoerner to this mailing list however there is no reply.
> > > > > >
> > > > > > Any help is greatly appreciated.
> > > > > >
> > > > > > --
> > > > > > Regards,
> > > > > > BJ Low
> > > > >
> > > > > > _______________________________________________
> > > > > > Concurrency-interest mailing list
> > > > > > Concurrency-interest at altair.cs.oswego.edu
> > > > > >
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Regards,
> > > > BJ Low
> > > >
> > >
> > >
> >
> >
> >
> > --
> > Regards,
> > BJ Low
> >
> >
>
>
> --
> Regards,
> BJ Low
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080104/d30c6263/attachment-0001.html 


More information about the Concurrency-interest mailing list