I apologize in advance if this has already been asked before but I didn't find it when I searched.
Referring to pg 680 of k&b on threads (exam watch), they mention that: since the Thread class implements Runnable, it's perfectly legal to pass it into a Thread constructor but that it's overkill. I can conceptualize that creating a custom Thread is going to be more expensive than an implementer of Runnable and that's why they say it's silly and overkill. However, exactly what happens, from a resource perspective, when we create a 'new' Thread instance vs. a new Runnable instance? Exactly how much more memory/resources, etc., are being wasted?
The "overkill" comes from (a) the additional memory required to support Thread's data members, which you won't be using if you're just using it as a Runnable; and (b) the additional constructor code that needs to be executed.
If you have JProfiler or some other instrumentation tool handy, you can take a look at exactly how much memory a Thread object consumes in your Java implementation. Alternatively, if you just want to get some idea of what Thread's data members and constructors may look like, the GNU Classpath source code for Thread should be fairly representative.
Joined: Oct 09, 2007
Perfect explanation Kevin...you answered my question AND gave me proof. I've seen your posts here, and you are truly blessing to this forum! Oddly, enough, since my last post I noticed that the K&B offers an example where a Calculator thread is passed into three Reader Thread constructors on page 725. However, this is still a WIP as they subsequently mention that the program is flawed because of the fact that the waiting threads (Readers) may wait forever if the notifying thread (Calculator) notifies before the Reader start. After checking out the link to DocJar you gave above (great..thanks!), I also looked at Runnable which just has the void run() (Thread also had imports (WeakHashMap and Map) plus the several members you mentioned. So perhaps the K&B example on pg 724-5 could be further improved by making at least the Calculator class (and ideally the Reader's too) implement Runnable...yippie, I'm learning to apply this stuff! Thanks again Kevin, your a godsend!