• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.currentThread()

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen somewhere on java.sun.com the following piece of code



and I am wondering, why there is a need to add the "currentThread()"?
I mean, why not just Thread.sleep(3000) ?
Whenever this come to execute, Thread.sleep(3000) will effect the current thread anyway.

Am I missing something?

Also, sleep() is static, so it's nice to call it on the class, not on an instance.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not missing anything. Whoever wrote the code was smoking crack or something.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph Sweet:
....and I am wondering, why there is a need to add the "currentThread()"?
I mean, why not just Thread.sleep(3000) ?

I used to worry about stuff like this to the point of losing sleep, and still code using the expanded style - but found out compilers excel at recognizing when there is no meaningful difference between two ways of coding the same effect. Use whichever way you find easiest to code.
Am I missing something?

No, except for some things you may want to miss.

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just that since it's a static method, you are supposed to get the same effect even when you write something like

 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph Sweet:
It's just that since it's a static method,....

I don't understand what static has to do with it. Static or Instance, it is a pattern of impulses laid out in cache that is brought in to the central processing board and jump to it's entry point - No ? Then whether it is on the heap or not should be an issue for a compiler writer / os designer - No ?

The only dumb question is one you should have asked and didn't.
 
Oliver Kamps
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas, what does heap have to do with it? A static method is one that is not invoked with respect to a specific object. So Joseph was right, would have done the trick.

Cheers,
Oliver
 
Henry Wong
author
Sheriff
Posts: 22524
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what static has to do with it. Static or Instance...


What Joseph is refering to, is in the realm of "best practices". Agreed that whether you use the "short form" or the "longer form", it should make very little difference -- just one extra method call, which just spends a few cycles to return a reference to the current thread object.

However, one of the common practices for calling static methods is to reference it by the class name instead of via an instance. The reasoning is, you as a developer is assuming the method is static, and you will generate an error, if you assumed wrong. Furthermore, IDEs generally provides the option of flaging the calling of static methods via an instance, to help with this.

I don't have a very strong opinion regarding this practice -- meaning I will follow this practice, but would not complain, if I see code that doesn't.

Henry
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Oliver Kamps]:Nicholas, what does heap have to do with it? A static method is one that is not invoked with respect to a specific object. So Joseph was rightI think I split some cycles here

I stand corrected, a fine distinction I wished to make has already been spotted by Master Wong and elucidated better than I could. (ps, I should have said instance rather than heap to remain within Java nomenclature) The intended effect of my question was to point out to original poster: Not missing anything ! .... without reliance on "best practices", such questions (IMO) need to stand rigorous inspection to thwart some double-blind blunders I have to deal with several times a day.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!