• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic question about Threads

 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get this down absolutely correctly. I'm learning about threads and the different ways to implement them (Runnable vs. Thread class). My question is this: If I have a class, and IN that class I 'spin off' a new thread, then do I have two threads? Is the 'running' class considered an anonymous thread before the new thread I create begins execution? I just want to make sure. It seems to me that a Java program, since it's taking resources, etc., would by default be a thread - just an unnamed one. Any answers or insight?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Your intuition is basically correct, but there's no such thing as an "anonymous thread." The thread in which main() is invoked is named "main" in most JVMs. Try running

A typical JVM has half a dozen or so threads running at startup. Here's the threads "jdb" says are running if I debug the program above:

SO there are two ThreadGroups with 5 threads in total, one of which is running NameTest.main(). If you create new threads from Thread "main", they end up in the "main" ThreadGroup.
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I to understand that, by default, new threads are in the main thread group. But what if I create a new Threadgroup...then would they be in THAT group instead? And I would have 3 thread groups running? (System, main, and whatever one I created?)
Thank you!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default (i.e. unless you use one of the constructors that specifies a ThreadGroup), a new Thread is in the same group as the thread that spawned it. Which is initially the main thread, yes. But if you create thread A in group Q, and then A creates B, C, D without specifying a group, then B, C, D will all be in group Q. If the main thread then creates E, F, and G, those will be in the main group, not Q.
[ January 26, 2004: Message edited by: Jim Yingst ]
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...thank you all...how about this one: I know that if I have a class that implements the Runnable interface, then I'm stating that I'm going to create a thread using "new Thread(this);" and define the run() method within that class. Do I still have one thread or two?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get two as soon as you call "start" on that new Thread object. Until then, you've only got one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!