• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread vs. Runnable

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would especially like Andrew Monkhouse's comments since he is the author of SCJD Exam with J2SE 5, but I also would like to hear from anyone that would like to respond.

I've noticed in the Threading chapter - the examples use Thread as opposed to Runnable. This is fine, of course, for illustration purposes in the text. My question is, for the exam, isn't it likely that assessors will look more favorable on our submitted projects if we use Runnables?

Thanks,
Steve
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both have their place, neither is fundamentally flawed or superior to the other.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again Jeroen is correct.

The API for Runnable does state that it should be used in preference to extending Thread, however you can weigh this up against the fact that your coding will be simpler to read if you subclass Thread (thereby helping out the junior programmer mentioned in the "Clarity and Maintainability section). Either choice is valid, but remember that it is a choice, and as such you might want to mention it in your choices document.

Was this a general question, or are you planning on creating a Sockets based solution?

Regards, Andrew
 
Manuel Comnenus
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeroen,

Thank you for your advice. By the way, I really appreciate your efforts to answer everyone's questions in this forum.

Steve


Hi Andrew,

One of the reasons that I am asking this - I am weighing the typical textbook advice against real-world and exam implementation issues that you address in your book. To me, the thread question is essentially a design decision.

I thought that there could have been some ambiguity as to why you took the approach to threading that you did - one reason being that it is more easier from an illustration point, another reason being that you possibly wanted the reader to reflect on the issue and independently "do the right thing".

On the sockets question...I have not made a decision on whether to take the SCJD certification, yet. I am using your book and this forum, at this point, to help me gauge whether I should. Intuitively, I would lean towards an RMI implementation since I don't have experience with it and RMI (at least theoretically) offers some very interesting functionality. As far as using it for my assignment - I would need to review the specs. If the specs were to state that I have no influence over the infrastructure and that there are going to be firewall or other considerations that are hostile to RMI, then I would start thinking of alternatives.

Regardless of where I am in my SCJD planning, could you elaborate on your thoughts regarding sockets and threads? It will help both me and a lot of other people who read this.

Thanks,
Steve
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

Could you please either change your displayed name to "Steve Comnenus" (or S Comnenus) or start signing your posts as Manuel? My poor little brain cant handle the names being different. I have no idea who I am talking to. You can change your displayed name here.

could you elaborate on your thoughts regarding sockets and threads?

Hmmm, I am not quite sure of what you are asking, but I will try to answer based on what I think the question is. If I am answering the wrong question, please just ask it again (hopefully slightly differently).

The reason I mentioned Sockets in my response above is that it is very rare for an SCJD candidate to start their own threads if they are building an RMI solution. If you search this forum for some discussions between Phil Maquet, Max Habibi and myself you might find one example where we felt that we could justify a daemon thread in an RMI solution using WeakHashMaps - but that was a case where we deliberately worked through a theoretical exercise to see where we would end up. None of us were interested in it from a practical point of view.

As for the choice between RMI and Sockets - it really is a design choice. They each have their good points, and each have their bad points. You will see that we do not favour either one over the other in the book, and indeed the project we developed works equally well with either network protocol.

Threads versus Runnable in the context of "real-world" versus "textbook" issue is not quite as clear-cut as many other similar issues. The standard textbook statement about subclassing often mentions the "is a" argument - is your class a specific subset of the super-class? In that context, you could easily argue for using the Runnable interface in preference to Thread - your class is not likely to be a thread, rather it is Runnable (or it runs in a thread). But creating new threads is probably the most common case where I see people extending a class where the new class does not fit the "is a" rule. Quite simply, for most use cases, coders find the theoretical distinction is too minor, while the improvement in coding clarity makes it worthwhile to break the rule.

Regards, Andrew
 
Manuel Comnenus
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,

My question was specific to the relative merits of threads versus runnables, the reasoning behind your decision in your book, and general advice on this for the assignment. I think that you answered this well.

The sockets issue was one that you had raised - in your response, you asked me whether I was thinking of a sockets implementation and therefore was my thread/runnable question related to that. I asked you to further comment on sockets (this is just an open-ended question on my part) so that I, and others that view this discussion thread, can get more of your perspective on the threads and/or sockets issues. If there is anything else that you can share on this, it would be most welcome (I am reading your book and therefore am not going to ask you to repeat what you have stated there).

Please accept my apologies for the confusion over my name. It is, in fact, Steve. My name was already taken and the naming conventions prohibit alphanumeric combos, so I quickly tested to see if the name of a 12th century character was taken. I could have, perhaps, picked a better character, but I didn't want to spend a lot of time when all I needed to do was pick a unique name. For now, I'll refrain from signing my posts with my name. I will consider picking a new name, but I'd like to wait until sometime that I don't feel like I'm in as much of a rush.
[ May 19, 2006: Message edited by: Manuel Comnenus ]
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic