is it possible to run JVM in a pthread?

Discussion in 'Java' started by Charles T. Smith, Apr 26, 2007.

  1. Does anyone know if it's possible to run a JVM in a pthread?

    Are there obstacles to doing that, like requirements for non-thread-safe
    api's?

    How about performance, if you have hundreds of such threads? Has anyone
    experience with that? Would it necessarily be less efficient that
    hundreds of java threads? Or more efficient?
    Charles T. Smith, Apr 26, 2007
    #1
    1. Advertising

  2. Charles T. Smith

    Daniel Pitts Guest

    On Apr 25, 10:57 pm, "Charles T. Smith" <> wrote:
    > Does anyone know if it's possible to run a JVM in a pthread?
    >
    > Are there obstacles to doing that, like requirements for non-thread-safe
    > api's?
    >
    > How about performance, if you have hundreds of such threads? Has anyone
    > experience with that? Would it necessarily be less efficient that
    > hundreds of java threads? Or more efficient?


    pthreads are POSIX threads, right? What dodes that have to do with
    running the JVM?

    Modern JVMs can use platform specific (a.k.a native) threads, or green
    threads of necessary. "What is the efficiency for a number of
    threads?" is a trick question. It depends on a lot of factors,
    including number of CPU cores, platform, what the threads are doing,
    amount of available memory, etc...

    If you really want to know more about concurrent programming, I
    recommend the book Java Concurrency In Practice <http://
    JavaConcurrencyInPractice.com/> It tells you all you need to know
    about thread-safety and the thread safety of common Java API's, as
    well as creating your own efficient multi-threaded programs.

    Hope this helps,
    Daniel.
    Daniel Pitts, Apr 26, 2007
    #2
    1. Advertising

  3. Charles T. Smith

    Eric Sosman Guest

    Charles T. Smith wrote On 04/26/07 01:57,:
    > Does anyone know if it's possible to run a JVM in a pthread?


    Not quite sure what you mean. If you ask "On a platform
    that provides pthreads, can a multi-pthreaded program start
    a JVM in one of its pthreads?" the answer is "Yes." If you
    mean "On a platform that provides pthreads, is it possible
    to run the JVM in just one pthread?" the answer is "Probably
    not." In earlier times there were JVM's that used native
    threads and JVM's that used "green threads," but my impression
    is that most contemporary JVM's -- probably all JVM's that
    use pthreads at all -- will themselves launch multiple
    pthreads and hence not remain confined to just one.

    And then, of course, there are platforms where pthreads
    aren't supported or aren't supported well enough for a JVM
    to use.

    > Are there obstacles to doing that, like requirements for non-thread-safe
    > api's?


    I can't think of any a priori reason why a JVM (in the
    abstract, as it were) would require thread-unsafe features.
    That's not to say that some particular JVM implementation
    doesn't use something dodgy; you'd need to get into the
    details of that particular JVM.

    > How about performance, if you have hundreds of such threads? Has anyone
    > experience with that? Would it necessarily be less efficient that
    > hundreds of java threads? Or more efficient?


    The ability to run a JVM alongside other non-JVM threads
    in a multi-threaded program is one thing, but the ability to
    run multiple non-interfering JVM's in a single address space
    is something quite different. I have not investigated the
    possibility, but I would be more than a little surprised if
    you were able to get even two JVM's to run independently in
    a single address space -- not "flabbergasted," but "more than
    a little surprised." The JVM implementations I know of play
    all manner of underhanded trickery to make things work, and
    it's quite possible that at least some of the sleight-of-RAM
    would need to be single-instance.

    As for efficiency -- well, it's hard to say. If the JVM's
    are truly non-interfering they'll load all their core classes
    independently, JIT them independently, build redundant class
    hierarchies, and so on: At the very least, they'll use more
    memory than the same "business" threads running in a single
    JVM. Memory may be cheap, but cache memory is both expensive
    and hard to expand, and increasing the pressure on caches is
    one good way to slow down a CPU: bigger working sets usually
    mean poorer performance. Of course, you'd have the same kind
    of memory bloat -- perhaps worse -- if you ran all those JVM's
    in independent processes. Running multiple threads inside a
    single JVM will probably make the best use of memory.

    Still, I get nervous about "hundreds of threads." It may
    be justifiable in your situation (whatever it is), but such a
    large thread count is often a sign that the design needs some
    rearranging. What problem are you trying to solve?

    --
    Eric Sosman, Apr 26, 2007
    #3
  4. On Thu, 26 Apr 2007 14:03:00 -0400, Eric Sosman wrote:

    > Charles T. Smith wrote On 04/26/07 01:57,:
    >> Does anyone know if it's possible to run a JVM in a pthread?

    >
    > Not quite sure what you mean. If you ask "On a platform
    > that provides pthreads, can a multi-pthreaded program start a JVM in one
    > of its pthreads?" the answer is "Yes." If you mean "On a platform that
    > provides pthreads, is it possible to run the JVM in just one pthread?" the
    > answer is "Probably not." In earlier times there were JVM's that used
    > native threads and JVM's that used "green threads," but my impression is
    > that most contemporary JVM's -- probably all JVM's that use pthreads at
    > all -- will themselves launch multiple pthreads and hence not remain
    > confined to just one.
    >
    > And then, of course, there are platforms where pthreads
    > aren't supported or aren't supported well enough for a JVM to use.
    >
    >> Are there obstacles to doing that, like requirements for non-thread-safe
    >> api's?

    >
    > I can't think of any a priori reason why a JVM (in the
    > abstract, as it were) would require thread-unsafe features. That's not to
    > say that some particular JVM implementation doesn't use something dodgy;
    > you'd need to get into the details of that particular JVM.
    >
    >> How about performance, if you have hundreds of such threads? Has anyone
    >> experience with that? Would it necessarily be less efficient that
    >> hundreds of java threads? Or more efficient?

    >
    > The ability to run a JVM alongside other non-JVM threads
    > in a multi-threaded program is one thing, but the ability to run multiple
    > non-interfering JVM's in a single address space is something quite
    > different. I have not investigated the possibility, but I would be more
    > than a little surprised if you were able to get even two JVM's to run
    > independently in a single address space -- not "flabbergasted," but "more
    > than a little surprised." The JVM implementations I know of play all
    > manner of underhanded trickery to make things work, and it's quite
    > possible that at least some of the sleight-of-RAM would need to be
    > single-instance.
    >
    > As for efficiency -- well, it's hard to say. If the JVM's
    > are truly non-interfering they'll load all their core classes
    > independently, JIT them independently, build redundant class hierarchies,
    > and so on: At the very least, they'll use more memory than the same
    > "business" threads running in a single JVM. Memory may be cheap, but
    > cache memory is both expensive and hard to expand, and increasing the
    > pressure on caches is one good way to slow down a CPU: bigger working sets
    > usually mean poorer performance. Of course, you'd have the same kind of
    > memory bloat -- perhaps worse -- if you ran all those JVM's in independent
    > processes. Running multiple threads inside a single JVM will probably
    > make the best use of memory.
    >
    > Still, I get nervous about "hundreds of threads." It may
    > be justifiable in your situation (whatever it is), but such a large thread
    > count is often a sign that the design needs some rearranging. What
    > problem are you trying to solve?



    Thank you, that was a very informative and entertaining answer. And
    complete. I'll give up on my idea. ;)
    Charles T. Smith, Apr 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Hooke

    Re: Handling both MS JVM and Sun JVM

    Kevin Hooke, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    865
  2. Lasse
    Replies:
    1
    Views:
    682
    Jon A. Cruz
    Jan 5, 2004
  3. R Mar
    Replies:
    0
    Views:
    392
    R Mar
    Jan 3, 2004
  4. Young-Jin Lee

    MS JVM and Sun JVM problem

    Young-Jin Lee, Jan 20, 2004, in forum: Java
    Replies:
    3
    Views:
    563
    Mickey Segal
    Jan 21, 2004
  5. Avin
    Replies:
    2
    Views:
    15,570
    Jerald Fijerald
    May 8, 2004
Loading...

Share This Page