• 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

Threads Queries

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
I have the below Thread related Queries.
Help Provided will be highly appreciated.

1> I'm new to Threading in java,Can anyone share few Multi threading examples which can help me to understand the concepts of threads in Java Better.?Practical Examples needed for better understanding.Please advice.?

2> Which is better to use ? Runnable Interface (or) Extending the thread class.

3> Can we Override a Run() method in Thread ?

--
Deepak Lal
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:Hi Ranchers,
I have the below Thread related Queries.
Help Provided will be highly appreciated.

1> I'm new to Threading in java,Can anyone share few Multi threading examples which can help me to understand the concepts of threads in Java Better.?Practical Examples needed for better understanding.Please advice.?

2> Which is better to use ? Runnable Interface (or) Extending the thread class.

3> Can we Override a Run() method in Thread ?

--
Deepak Lal



I really don't have any examples now (unless you don't mind if they are written in Portuguese), but to your other questions:

2 - I usually implement Runnable... this way you are still able to extend other classes.

3 - You actually should if you want anything running And one more to implementing Runnable, no confusions about what to implement!
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(unless you don't mind if they are written in Portuguese).


Hmmm a very impolite answer for the Question1 Marcelo Schneider. :roll:

2> Any other reasons as to why we should implement runnable interface as opposed to extending a thread class,do you know any other reasons apart from that ??

3> You mean to say that we need to override run() method in order to get the thread running.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hmmm a very impolite answer for the Question1 Marcelo Schneider.



How is this an impolite answer? He is simply stating that he codes in Portuguese. Did you expect him to port his code to English just as an example for you?

Keep in mind that we are all volunteers here. People take there own time, and effort, to answer your questions -- when obviously, they don't have to... When you answer questions for other users, how much of an extra effort do you make?

Henry
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Henry i got it. i misunderstood Aplogies to Marcelo Schneider.

Anyways can you please answer the below queries.

1> Anyone can share any multi threading Java (Practical Java examples) which could explain concepts of multi threading.

2> Any other reasons as to why we should implement runnable interface as opposed to extending a thread class,do you know any other reasons apart from that ??

3> You mean to say that we need to override run() method in order to get the thread running Please let me know if my understanding is correct or not.

--
Deepak Lal

 
Marcelo Schneider
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
How is this an impolite answer? He is simply stating that he codes in Portuguese. Did you expect him to port his code to English just as an example for you?



Actually I code in english, it looks better that way... it's just that every piece of multi-threaded code I had in hands were examples from other (brazilian) forum I contribute. And of course, even though anyone can understand coding in any language - it's all Java after all - I didn't think it would be of much help as all the comments were not in english.

1 - Searching the names of the classes in java.util.concurrent in Google Code Search gave me some very interesting results, and you can always trust the Java Tutorials...

2 - No, actually. It is just a mater of preference. I just think it looks cleaner that way, I'm not aware of any performance degradation if that's what you mean.

3 - Yep.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean implement the run () method of the Runnable interface?

There is in fact a good reason for implementing the Runnable interface rather than extending Thread: you can implement several interfaces but only extend one class.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answers and valuable input..

--
Deepak Lal
 
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Deepak Lal wrote:2> Which is better to use ? Runnable Interface (or) Extending the thread class.


Campbell Ritchie wrote:There is in fact a good reason for implementing the Runnable interface rather than extending Thread: you can implement several interfaces but only extend one class.


Here is just one more reason for implementing Runnable interface: you cannot start the same instance of Thread class more than once, but you can start new threads with the same instance of a class which implements Runnable interface.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Alexey Saenko wrote:
Here is just one more reason for implementing Runnable interface: you cannot start the same instance of Thread class more than once, but you can start new threads with the same instance of a class which implements Runnable interface.



Hi Alexey Saenko,
I'm new to threads in Java.If you could please elborate the above statement with an example.it would help in understanding the concepts a bit more better.


Help provided will be highly appreciated.

--
Deepak Lal
 
Alexey Saenko
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really, maybe my argument was too short I'll try to explain it more detailed, but firstly new argument and some introduction.

In words of multithreading, Thread is something what must do some kind of job and Runnable implementator is a some kind of job. They have different purpose, therefore, from design and software architecture point of view, it's better to split them to two different classes.
And some explanations for my previous argument..

Alexey Saenko wrote:but you can start new threads with the same instance of a class which implements Runnable interface.


In other words, you can give THE SAME JOB to DIFFERENT WORKERS and this job can be done multiple times.

Hope, it will help a bit
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alexey Saenko,
Thanks for your valuable input and information.
Could you please provide me a Java Example to justify the below statement which you have made.If you can illustrate it with an example.it would help a lot.

Here is just one more reason for implementing Runnable interface: you cannot start the same instance of Thread class more than once, but you can start new threads with the same instance of a class which implements Runnable interface.



Example provided will be highly appreciated.

--
Deepak Lal
 
Alexey Saenko
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak Lal,

Here is the simplest java example:

 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the example.



--
Deepak Lal
 
reply
    Bookmark Topic Watch Topic
  • New Topic