Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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

0
wsyy
Asked:
wsyy
  • 3
  • 3
1 Solution
 
CEHJCommented:
The best way is to set the thread name explicitly first, before using the code you've got
0
 
objectsCommented:
btw you're not starting a new thread, you need to call start() to do that
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
wsyy, can you say why you accepted that answer? It answers a different question
0
 
objectsCommented:
> 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
0
 
CEHJCommented:
?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now