• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Generic question about Threads

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic