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


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

Re: java thread question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: java thread question
Date Thu, 03 Apr 2014 11:43:54 -0400
Organization A noiseless patient Spider
Lines 30
Message-ID <lhjvju$t97$1@dont-email.me> (permalink)
References <ffb65076-f551-432b-b548-303a884abd44@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 3 Apr 2014 15:43:58 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="29991"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oGHJXPsXFIXiBJA5M/h74"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
In-Reply-To <ffb65076-f551-432b-b548-303a884abd44@googlegroups.com>
Cancel-Lock sha1:Z3X5g+sO5N6ZI571wwF6skkUVdU=
Xref csiph.com comp.lang.java.programmer:27944

Show key headers only | View raw


On 4/3/2014 10:51 AM, mcheung63@gmail.com wrote:
> Hi All
>      I have write a little program to keep running some threads, i got questions:
> 1) All threads are running within one process (ps command show me this), but it will eat up all cpu cores, how to explain this?

     What needs explanation?  That is, what puzzles you?  The *purpose*
of threads is to break up a large task into smaller pieces and let them
execute in parallel; you *want* those smaller pieces to use whatever
machine resources they can get.

> 2) If thread will eat multiple cpu core, why wee need fork/join in java?

     (I assume you're referring to java.util.concurrent.ForkJoinTask.)
If your large task subdivides into a million smaller pieces, you don't
want to try running each piece in a separate thread.  A thread is
"lighter-weight" than a full-scale process, but its weight is non-zero:
There's a stack, there's other CPU context, there's management overhead
like schedulers and garbage collectors, ...  When you multiply this by
a million, or even by several thousand, your system will collapse under
the load, most probably by throwing an OutOfMemoryException.

     That's where ForkJoinPool and ForkJoinTask come in.  The pool runs
a manageable number of long-lived threads, and each thread executes many
ForkJoinTasks in succession.  (There are additional intricacies, but
that's the essence.)  You don't have to create, start, shut down, and
garbage-collect a million threads to get the million sub-tasks done.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

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


Thread

java thread question mcheung63@gmail.com - 2014-04-03 07:51 -0700
  Re: java thread question Nigel Wade <nmw@ion.le.ac.uk> - 2014-04-03 16:17 +0100
  Re: java thread question Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-03 11:43 -0400
    Re: java thread question mcheung63@gmail.com - 2014-04-03 15:10 -0700
      Re: java thread question Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-03 19:00 -0400
        Re: java thread question mcheung63@gmail.com - 2014-04-04 09:29 -0700
  Re: java thread question Roedy Green <see_website@mindprod.com.invalid> - 2014-04-03 17:52 -0700
    Re: java thread question mcheung63@gmail.com - 2014-04-04 09:31 -0700
    Re: java thread question Roedy Green <see_website@mindprod.com.invalid> - 2014-04-04 09:49 -0700
      Re: java thread question mcheung63@gmail.com - 2014-04-05 09:41 -0700

csiph-web