Newbie question on multithreads

M

Michelle

Hi all,

I'm learning java right now and when I come to
threads (Thread class), I have some questions to
clarify before I move on.

I tried and found that the Thread.getName() can
give me the name of curren thread. However,
is it possible to check if some other threads
exist before I create it? For example, my current
thread is named Thread_1 and I want to know if
Thread_2 has been created before I start one.

Besides, is it also possible to know which threads
exists before I quit my program? Probably by name,
count, or whatever?

Thanks.
 
M

Michael Borgwardt

Michelle said:
I tried and found that the Thread.getName() can
give me the name of curren thread. However,
is it possible to check if some other threads
exist before I create it? For example, my current
thread is named Thread_1 and I want to know if
Thread_2 has been created before I start one.

Besides, is it also possible to know which threads
exists before I quit my program? Probably by name,
count, or whatever?

Both are only possible with limitations, namely that you can only
get information about threads in the same ThreadGroup. Look at
the API doc for details.

However, why do you even care? It's generally not necessary to worry
about stuff like that.
 
S

Soeren Degn Jahns

I tried and found that the Thread.getName() can
give me the name of curren thread. However,
is it possible to check if some other threads
exist before I create it? For example, my current
thread is named Thread_1 and I want to know if
Thread_2 has been created before I start one.

I would consider having some kind of singleton class to
keep track of your threads. Everytime a thread is created/destroyed
your ThreadManager should be notified. However, it all depends
on how your application should work.

Besides, is it also possible to know which threads
exists before I quit my program? Probably by name,
count, or whatever?

If you associate threads with a ThreadGroup (java.lang.ThreadGroup)
then you could use the "int activeCount()" method that will return an
estimate of the number of threads in the group.


// Soeren
 
L

Liz

Soeren Degn Jahns said:
I would consider having some kind of singleton class to
keep track of your threads. Everytime a thread is created/destroyed
your ThreadManager should be notified. However, it all depends
on how your application should work.



If you associate threads with a ThreadGroup (java.lang.ThreadGroup)
then you could use the "int activeCount()" method that will return an
estimate of the number of threads in the group.


// Soeren
I use this to get a list of threads in my thread group which
happens to be named "System", but when I run the debugger I find that
there is another standard thread group called "main" that I
don't know how to get at.

int tcount =
Thread.currentThread().getThreadGroup().getParent().enumerate(myThreadList,
true);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top