This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "threads" Watch "threads" New topic
Author

threads

sathiya moorthy
Greenhorn

Joined: Nov 27, 2004
Posts: 9
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
siv sanny
Greenhorn

Joined: Jan 03, 2005
Posts: 8
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

Joined: Jan 29, 2003
Posts: 8791


Threads!

Sorry, I must be really bored.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
 
subject: threads
 
Similar Threads
confusion in ThreadLocal example.
Another Question (.... Threads / join() )
Sharing of Memory in Threads?
locking??
Need Explanation of unpredicted output even if methods are synchronized