[pypy-dev] Thread/gil/java question

Armin Rigo arigo at tunes.org
Sun Jan 1 13:48:32 CET 2006


On Fri, Dec 30, 2005 at 12:47:38AM +0100, Christian Tismer wrote:
> you already got a very good answer, since you happened to hit
> the former maintainer of Jython.

Christian's answer is a bit too harsh IMHO.  I don't think we should try
to keep open discussions off pypy-dev, at least as long as they don't
become long threads where every answer is just uninformed guesses and
wild speculations building on each other.  I don't see pypy-dev in
danger of becoming that at the moment.

> >Thank you for the quick response.  Let me get this straight:  If I use
> >Jython instead of CPython, then my Python threads can migrate to
> >different CPUs?


> >Not sure what you meant by "... granularity is such that not much could
> >be assumed for user programs..."--could you clarify that a bit?

In CPython, if say 'd' is a dict, then 'd.setdefault(x,y)' is atomic, so
the result cannot be messed up by another thread accessing 'd' at the
same time.  In Java you don't have complex operations, so you (or a Java
library) would have to write code like:

  if (d.hasKey(x)) {
    return d.get(x);
  else {
    d.set(x, y);
    return y;

Because this is user Java code, in a multithreaded program you need
explicit locks around it.  The "granularity" of Java is finer than
Python, so even if individual Java operations (e.g. hasKey(), possibly)
are atomic, no higher-level operation is intrinsically atomic.

In Jython, all dict methods lock the dict (and similarily for all
standard types), which gives the Python program again the illusion that
the methods are atomic.

A bientot,


More information about the Pypy-dev mailing list