[concurrency-interest] ConcurrentHashMap NullPointerException

David Holmes dcholmes at optusnet.com.au
Fri Jan 4 02:27:50 EST 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080104/657c2116/attachment-0001.html 


More information about the Concurrency-interest mailing list