• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

threads  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When run this program i am getting thread count =2, any tell what are those threads and how it's two?

public class thread1 {
public thread1() {
}
public static void main(String[] args) {
Thread t=Thread.currentThread();
System.out.println("Current thread "+t);
t.setName("my thread");
System.out.println("count "+t.activeCount());
}
}
System.out.println("count "+t.activeCount());

[ EJFH: Edited for spelling ]
[ January 03, 2005: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Essentially all JVMs have multiple threads running at startup, but the details vary from implementation to implementation. Typically, there are separate threads dedicated to running the garbage collector and calling finalizers. If the AWT is loaded, then there will be more threads running the event queue and other GUI-related services.

You can call t.getThreadGroup() to get the ThreadGroup for your main thread, and then use one of the enumerate() methods to get a list of those two Threads. You can then print each Thread and see its description. All the JVM-created threads should have a descriptive name so you can tell what they are.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just playing with JConsole in JDK 5 and it has a neat view of running threads. This article talks about it: http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This is Extension Q. to post
I have Emnumerated as said but still only one Thread
as output shows.

Below is the main() method.

public static void main(String[] args)throws Exception
{
Thread threadList[] = new Thread[4];
MyThread myThread = new MyThread();
Thread t = Thread.currentThread();

System.out.println("Current:"+t);
t.setName("Test");
System.out.println("After Name Change:"+t);

System.out.println("Active Thread Coutn:"+Thread.activeCount());

ThreadGroup tg = t.getThreadGroup();
tg.enumerate(threadList,true);
for(int i = 0; i< threadList.length;i++ )
{
System.out.println("Thread ["+i+"]is: "+threadList[i]);
}
}




//OutPut

Current: Thread[main,5,main]
After Name Change:Thread[Test,5,main]
Active Thread Coutn: 2

thread [0] is: Thread[Test,5,main]
thread [1] is: null
thread [2] is: null
thread [3] is: null

some please clarify this for ....
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Threads!

Sorry, I must be really bored.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sankar vas:
Hi all,
This is Extension Q. to post
I have Emnumerated as said but still only one Thread
as output shows.



Are you the same person who posted the first question? I'm confused.

In any case, this program is totally different than the first one, as it creates a Thread object (presumably MyThread is a subclass of Thread). Thread.activeCount() returns not the exact number of active threads, but an estimate; see the Javadocs for ThreadGroup.activeCount(). That estimate can be higher than the actual number of running threads; in particular, it can include Thread objects created but not yet started. When you call enumerate(), you get only those threads that are actually started.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!