Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

currentthread().getName() not returning name  RSS feed

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started off with this program just displaying the Thread no with the static member threadCount. The program worked ok and was getting the desired output.Initially the Thread constuctor in the main metod was just
Thread counter =new Thread(newThread).I have added an extra argument to the Thread constuctor of type String and looped through an array of names , which I thought should be called when the currentThread()getName() was assigned to the String name in the subclass consructor but the output I am getting is
thread 1 is being created initValue = 3//this number differs as it israndom
main is name //this is where the name should be displayed instead of "main"
thread 2 is being created initvalue = 4 //again random
main is name//this should be a different name
and so on
code someone please help with an explanation -- heres the code
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only place you're printing the thread name is in the Counter constructor. At the time this constructor is being called, the new Thread that will run it hasn't even been created yet, which means of course that it hasn't been started. The Counter constructor is being called from within the main thread, so that's the name you're seeing. If you put some code into the run() method to print the name of the current thread, you will see different results - because run() is called as part the new thread(s), after start() has been called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!