I know that the getName() function on the main thread will return the String main, but this can be changed with setName().
Is there any way to always determine the main thread of an application?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It seems that the main thread has an id of
1as indicated byThread.getId():I’m not sure if it is part of the specification or an implementation-specific feature.
A more portable way is this:
with the caveat that
mainThreadIdhas to be either in a class that is loaded by the main thread (e.g. the class containing themainmethod). For instance, this doesn’t work: