Link to home
Start Free TrialLog in
Avatar of wsyy
wsyy

asked on

How to find out a Java thread name

Hi,

I would like to print the thread caller's names after the threads start. To be more clear, I have three code snippets attached. The first basically calls the run() function to run which in turn calls the simulate() function.

I would like to print in the simulate() which instance, ts or ts1, is call the function of run().

Thanks.



 
private void Simulate(){
		System.out.println(Thread.currentThread().getName()+" starting...");
		....
}

Open in new window

public class TestThreads {
	
	public static void main(String[] args){
		for(int i=0; i<200; i++){
			YYTConfiguration yytConf=new YYTConfiguration();
			MyThread ts=new MyThread(yytConf.getConfiguration());
			MyThread ts1=new MyThread(yytConf.getConfiguration());
			ts.run();
			ts1.run();
		}
	}
}

Open in new window

public void run(){
		Simulate();
	}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The best way is to set the thread name explicitly first, before using the code you've got
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw you're not starting a new thread, you need to call start() to do that
wsyy, can you say why you accepted that answer? It answers a different question
> wsyy, can you say why you accepted that answer? It answers a different question

No it doesn't. I don't think you read the question properly
?