• 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 or Runnable

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand implementing Runnable is preferable to extending Thread. In one of the Java sites I came across the following statement:
"The second way is to create the thread and supply it an object with a run() method. This object will be permanently associated with the thread. The object's run() method will be invoked when the thread is started. This method of thread creation is useful if you want many threads sharing an object."

Whats does "..many thread sharing an object mean" if the class is listening on a tcp socket? If I make 5 instances of a class that is listening on a tcp socket and implements Runnable, are there 5 sockets or 1 socket shared between 5 instances?
Would the bytes coming in get split between 5 instances?
Apologise for the real dumb question.

regards
Ravi
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Shankarappa wrote:
I understand implementing Runnable is preferable to extending Thread.



Yes. About the only time it makes sense to extend Thread is for an anonymous inner class when you don't want the extra layer of the anonymous Runnable implementation. And really, that's just convenience, not good design.


Whats does "..many thread sharing an object mean"



They're talking about the case where your Runnable is structured such that it makes sense for multiple threads to invoke the run() method on the same instance of your Runnable. For this to work, either it must not maintain any state of its own, or access to that state must be properly synchronized, and it must make sense for multiple threads to be access/updating that state.

if the class is listening on a tcp socket? If I make 5 instances of a class that is listening on a tcp socket and implements Runnable, are there 5 sockets or 1 socket shared between 5 instances?



That depends on how you write yoru class. Having one Socket shared among multiple threads would probably be a bad idea though (except for one thread to read and one thread to write).

Would the bytes coming in get split between 5 instances?



It depends on how you write the code to read from the Socket.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic