This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Silly thread question re: resources needed to create a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Silly thread question re: resources needed to create a Thread" Watch "Silly thread question re: resources needed to create a Thread" New topic
Author

Silly thread question re: resources needed to create a Thread

nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
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?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Silly thread question re: resources needed to create a Thread
 
Similar Threads
doubt regarding Threads?
Threads
threads query
Multiple Threading
Why subclass a Thread