aspose file tools*
The moose likes Threads and Synchronization and the fly likes status of deamon threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "status of deamon threads" Watch "status of deamon threads" New topic
Author

status of deamon threads

Ashok Reddy
Greenhorn

Joined: Feb 27, 2007
Posts: 5
challenge is to knowing currently running Deamon threads and their status like waiting,Stopped etc in the current JRE/JVM wothout touching application.Any one has any idea...please help?
[ January 15, 2008: Message edited by: ashok kumar reddy R ]

AshokReddy
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

What do you mean by "without touching the application"?
You mean i can write some extra code that runs in the same JVM and not the application OR you want a code running in an external JVM to query the JVM in question for deamon threads?


apigee, a better way to API!
Ashok Reddy
Greenhorn

Joined: Feb 27, 2007
Posts: 5
Hi Nitesh,

Thank you much for the response.

It would be great if it is writing code where external JVM accesses actual application-JVM and getting the deatils of deamon threads and their current state.I think that may not be possible.It is okay even if we write some code in the same JVM where application is running and getting the status of the deamon threads.

Thanks in advance.

AshokReddy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
Sounds like a case for JMX. If the JConsole tool that comes with the JDK can monitor JVM threads externally, then so can any other tool you care to develop. See the JEE FAQ for some links about JMX.

Inside of the JVM it's much easier. The following code iterates through all threads; it's just a matter of using the Thread objects to get at information you're interested in.


Ping & DNS - my free Android networking tools app
Ashok Reddy
Greenhorn

Joined: Feb 27, 2007
Posts: 5
Hi shariff,

Thanks much for the response.

I have created two java files of which one is threads generating program (some user threads) and other program has the code that you have sent(call as threads monitoring program).I ran them at the same time in two individual command prompts.but both programs running on the same machine(same JVM) and so the generated threads count should be shown in thread monitoring program, but that is not happening.what could be the reason.I am adding those two programs:

Threads Monitoring Program:

class DeamonThread{

private void getDeamonsStatus(){
ThreadGroup tg = getThreadGroup(Thread.currentThread().getThreadGroup());
// Get the count of active threads in the system
int num = tg.activeCount();
System.out.println("Total threads found ->"+num);
// Get all of the active threads in the system
Thread list[] = new Thread[num];
int x = tg.enumerate(list);
for (int i = 0; i < x; i++)
{
if (list[i] == null) continue;
String threadName = list[i].getName();
String groupName = list[i].getThreadGroup().getName();
System.out.println("GroupName----"+groupName);
System.out.println("ThreadName --- " + threadName);
System.out.println("Priority --- " + list[i].getPriority());
System.out.println("Interrupted --- " + list[i].isInterrupted());
System.out.println("Is Deamon thread --- " + (list[i].isDaemon() ? "Yes" : "No") );
System.out.println("Is thread Alive --- " + (list[i].isAlive() ? "Yes" : "No"));

}

}

// Recursively calls itself to find the top level thread group

private ThreadGroup getThreadGroup (ThreadGroup tg) {
ThreadGroup parent = tg.getParent();
if (parent == null)
return tg;
return getThreadGroup(parent);

}

public static void main(String[] args){

DeamonThread dt=new DeamonThread();
dt.getDeamonsStatus();

}

}



Threads Generation Program as follows:


class ThreadsGeneration{
public static void main(String[] args){
ThreadGroup tg1=new ThreadGroup("ThreadGroup1");
Thread1 g1t1=new Thread1();
Thread t1=new Thread(tg1,g1t1,"Group1Thread1");
t1.setDaemon(true);
t1.start();
Thread1 g1t2=new Thread1();
Thread t2=new Thread(tg1,g1t2,"Group1Thread2");
t2.setDaemon(true);
t2.start();
Thread1 g1t3=new Thread1();
Thread t3=new Thread(tg1,g1t3,"Group1Thread3");
t3.setDaemon(true);
t3.start();


ThreadGroup tg2=new ThreadGroup("ThreadGroup2");
Thread2 g2t1=new Thread2();
Thread t21=new Thread(tg2,g2t1,"Group2Thread1");
t21.setDaemon(true);
t21.start();
Thread2 g2t2=new Thread2();
Thread t22=new Thread(tg2,g2t2,"Group2Thread2");
t22.setDaemon(true);
t22.start();
Thread2 g2t3=new Thread2();
Thread t23=new Thread(tg2,g2t3,"Group2Thread3");
t23.start();


ThreadGroup tg3=new ThreadGroup("ThreadGroup3");
Thread3 g3t1=new Thread3();
Thread t31=new Thread(tg3,g3t1,"Group3Thread1");
t31.start();
Thread3 g3t2=new Thread3();
Thread t32=new Thread(tg3,g3t2,"Group3Thread2");
t32.start();
Thread3 g3t3=new Thread3();
Thread t33=new Thread(tg3,g3t3,"Group3Thread3");
t33.start();
try{
Thread.currentThread().sleep(60000);
}catch(InterruptedException ie){
System.out.println("Some one disturbed me while I was sleeping");
}


}
}

class Thread1 implements Runnable{

public void run(){
for(int i=1;i<=10;i++){
System.out.println("Thread1->"+i);
try{
Thread.sleep(3000);
}catch(InterruptedException ie){
System.out.println("Exception in thread 1");
}
}
}
}

class Thread2 implements Runnable{
public void run(){
for(int i=101;i<=110;i++){
System.out.println("Thread2->"+i);
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.out.println("Exception in thread 2");
}
}
}
}

class Thread3 implements Runnable{
public void run(){
for(int i=201;i<=210;i++){
System.out.println("Thread3->"+i);
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.out.println("Exception in thread 3");
}
}
}
}



thank you so much.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
If they're two programs then they will run in two different JVMs, and thus the thread monitor will monitor the treads in its own JVM, not the other JVM which runs the user threads.

Just because two programs run on the same machine does not mean that they run in the same JVM. That's what I meant with "inside the JVM" in my earlier post - that refers to "inside the JVM that runs the user code". If the monitor runs in its own JVM then some other method must be used (like JMX).

By the way, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is.
[ January 17, 2008: Message edited by: Ulf Dittmer ]
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by ashok kumar reddy R:
Hi shariff,

Name is Ulf Dittmer

Originally posted by ashok kumar reddy R:

I ran them at the same time in two individual command prompts.but both programs running on the same machine(same JVM)


When you run two different command prompts, two different JVMs will be spawned.
[ January 17, 2008: Message edited by: Nitesh Kant ]
Ashok Reddy
Greenhorn

Joined: Feb 27, 2007
Posts: 5
Hi Ulf Dittmer ,Nitesh

I am thinking that as long as I run any number of applications in the same machine then they all be under control of same JVM.can you give more info on Java Management Extension (JMX).
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Ashok: I am thinking that as long as I run any number of applications in the same machine then they all be under control of same JVM.

No thats not correct.

Ashok: can you give more info on Java Management Extension (JMX).


Did you see the link that Ulf has mentioned in his previous post?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
I am thinking that as long as I run any number of applications in the same machine then they all be under control of same JVM.

No. Every time the "java" command is used it starts a new JVM.

can you give more info on Java Management Extension (JMX)

Follow the link in my first post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: status of deamon threads