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


Groups > comp.lang.java.programmer > #27942 > unrolled thread

java thread question

Started bymcheung63@gmail.com
First post2014-04-03 07:51 -0700
Last post2014-04-05 09:41 -0700
Articles 10 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#27942 — java thread question

Frommcheung63@gmail.com
Date2014-04-03 07:51 -0700
Subjectjava thread question
Message-ID<ffb65076-f551-432b-b548-303a884abd44@googlegroups.com>
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?
2) If thread will eat multiple cpu core, why wee need fork/join in java?

public class TestThread implements Runnable {
	public static void main(String args[]) {
		for (int x = 0; x < 10; x++) {
			new Thread(new TestThread()).start();
		}
	}

	@Override
	public void run() {
		double r = 1.2323;
		while (true) {
			r = r * r + Math.pow(r, r);
		}
	}
}



thanks
from Peter (cmk128@hotmail.com)

[toc] | [next] | [standalone]


#27943

FromNigel Wade <nmw@ion.le.ac.uk>
Date2014-04-03 16:17 +0100
Message-ID<bq5cceF2nakU1@mid.individual.net>
In reply to#27942
On 03/04/14 15:51, 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?

Because the OS can normally schedule any runnable process/thread to run on any available CPU core.
If your process has sufficient threads which are runnable then every core will be used.
One of the main purposes of threads is that they offer concurrent execution.

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

Fork starts another process, and this may run an entirely different executable.

If your only goal is to consume CPU then you don't require fork.

[toc] | [prev] | [next] | [standalone]


#27944

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-04-03 11:43 -0400
Message-ID<lhjvju$t97$1@dont-email.me>
In reply to#27942
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

[toc] | [prev] | [next] | [standalone]


#27946

Frommcheung63@gmail.com
Date2014-04-03 15:10 -0700
Message-ID<8692d198-04b4-4b21-915e-b0630cf0c256@googlegroups.com>
In reply to#27944
Eric Sosman於 2014年4月3日星期四UTC+8下午11時43分54秒寫道:
> 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

Thanks, that mean the advantage of fork/join is : I don't need to manage a thread pool by myself.

[toc] | [prev] | [next] | [standalone]


#27947

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-04-03 19:00 -0400
Message-ID<lhkp72$6lt$1@dont-email.me>
In reply to#27946
On 4/3/2014 6:10 PM, mcheung63@gmail.com wrote:
> [...]
> [... more quoting than needed ...]
> [... all of it double-spaced  ...]
> [...]
> Thanks, that mean the advantage of fork/join is : I don't need to manage a thread pool by myself.

     Yes, largely.  Note, too, that the efficient management of task
counts in the millions is no easy matter, so another advantage is
"I don't need to *invent* a super-efficient thread pool myself."

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

[toc] | [prev] | [next] | [standalone]


#27952

Frommcheung63@gmail.com
Date2014-04-04 09:29 -0700
Message-ID<e4ff60a3-44ed-4368-a095-566303e41eb2@googlegroups.com>
In reply to#27947
Eric Sosman於 2014年4月4日星期五UTC+8上午7時00分46秒寫道:
> On 4/3/2014 6:10 PM, mcheung63@gmail.com wrote:
> 
> > [...]
> 
> > [... more quoting than needed ...]
> 
> > [... all of it double-spaced  ...]
> 
> > [...]
> 
> > Thanks, that mean the advantage of fork/join is : I don't need to manage a thread pool by myself.
> 
> 
> 
>      Yes, largely.  Note, too, that the efficient management of task
> 
> counts in the millions is no easy matter, so another advantage is
> 
> "I don't need to *invent* a super-efficient thread pool myself."
> 
> 
> 
> -- 
> 
> Eric Sosman
> 
> esosman@comcast-dot-net.invalid

Thanks Eric

[toc] | [prev] | [next] | [standalone]


#27948

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-04-03 17:52 -0700
Message-ID<9c0sj9li5pmdsvmp8ivesq8cuh9m6t31c8@4ax.com>
In reply to#27942
On Thu, 3 Apr 2014 07:51:16 -0700 (PDT), mcheung63@gmail.com wrote,
quoted or indirectly quoted someone who said :

>    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?
>2) If thread will eat multiple cpu core, why wee need fork/join in java?

what do you mean by "eat"?

Threads join the queue of processes to execute. the OS schedules them
on the next available CPU core.  So if you have 7 threads and 4 cores,
you can have 4 of those threads running at once.

Your threads compute just like independent jobs, though the OS can do
proprietary clever things.

When you exec/fork, you spin off a separate job than runs
independently in a separate address space.   The spawner can fail and
the child keeps going.  The child can be written in a different
language. The child have very limited ability to communicate with the
spawner.  A thread has access to the shared address space directly.

-- 
Roedy Green Canadian Mind Products http://mindprod.com
"The question of whether machines can think is about as relevant 
as the question of whether submarines can swim"
~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

[toc] | [prev] | [next] | [standalone]


#27953

Frommcheung63@gmail.com
Date2014-04-04 09:31 -0700
Message-ID<21295020-c0cb-4143-8c1e-d537c5dbd69c@googlegroups.com>
In reply to#27948
Roedy Green於 2014年4月4日星期五UTC+8上午8時52分54秒寫道:
> On Thu, 3 Apr 2014 07:51:16 -0700 (PDT), mcheung63@gmail.com wrote,
> 
> quoted or indirectly quoted someone who said :
> 
> 
> 
> >    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?
> 
> >2) If thread will eat multiple cpu core, why wee need fork/join in java?
> 
> 
> 
> what do you mean by "eat"?
> 
> 
> 
> Threads join the queue of processes to execute. the OS schedules them
> 
> on the next available CPU core.  So if you have 7 threads and 4 cores,
> 
> you can have 4 of those threads running at once.
> 
> 
> 
> Your threads compute just like independent jobs, though the OS can do
> 
> proprietary clever things.
> 
> 
> 
> When you exec/fork, you spin off a separate job than runs
> 
> independently in a separate address space.   The spawner can fail and
> 
> the child keeps going.  The child can be written in a different
> 
> language. The child have very limited ability to communicate with the
> 
> spawner.  A thread has access to the shared address space directly.
> 
> 
> 
> -- 
> 
> Roedy Green Canadian Mind Products http://mindprod.com
> 
> "The question of whether machines can think is about as relevant 
> 
> as the question of whether submarines can swim"
> 
> ~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

thanks Roedy

[toc] | [prev] | [next] | [standalone]


#27954

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-04-04 09:49 -0700
Message-ID<fkotj9dq0skqdid0mqbajlo30oiarmp63t@4ax.com>
In reply to#27948
On Thu, 03 Apr 2014 17:52:54 -0700, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>Threads join the queue of processes to execute. the OS schedules them
>on the next available CPU core.  So if you have 7 threads and 4 cores,
>you can have 4 of those threads running at once.

for more detail, see http://mindprod.com/jgloss/exec.html

I have updated the essay and will not upload it for an hour or so.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
"The question of whether machines can think is about as relevant 
as the question of whether submarines can swim"
~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

[toc] | [prev] | [next] | [standalone]


#27958

Frommcheung63@gmail.com
Date2014-04-05 09:41 -0700
Message-ID<37c8f95e-82b8-45b0-8911-355e95f9efc5@googlegroups.com>
In reply to#27954
Roedy Green於 2014年4月5日星期六UTC+8上午12時49分54秒寫道:
> On Thu, 03 Apr 2014 17:52:54 -0700, Roedy Green
> 
> <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
> 
> someone who said :
> 
> 
> 
> >Threads join the queue of processes to execute. the OS schedules them
> 
> >on the next available CPU core.  So if you have 7 threads and 4 cores,
> 
> >you can have 4 of those threads running at once.
> 
> 
> 
> for more detail, see http://mindprod.com/jgloss/exec.html
> 
> 
> 
> I have updated the essay and will not upload it for an hour or so.
> 
> -- 
> 
> Roedy Green Canadian Mind Products http://mindprod.com
> 
> "The question of whether machines can think is about as relevant 
> 
> as the question of whether submarines can swim"
> 
> ~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

Thanks for your link, your website is helpful

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web