File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Threads Queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads Queries" Watch "Threads Queries" New topic
Author

Threads Queries

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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


When The Going Gets Tougher,The Tougher gets Going
Marcelo Schneider
Greenhorn

Joined: Jan 10, 2009
Posts: 7
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

Joined: Jul 01, 2008
Posts: 507

(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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Jan 10, 2009
Posts: 7
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36475
    
  16
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

Joined: Jul 01, 2008
Posts: 507

Thanks for the answers and valuable input..

--
Deepak Lal
Alexey Saenko
Ranch Hand

Joined: Aug 18, 2008
Posts: 30

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.

SCJP 6, SCBCD 5, OCEWSD 6
My blog: http://darkleden.wordpress.com
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507


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

Joined: Aug 18, 2008
Posts: 30

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

Joined: Jul 01, 2008
Posts: 507

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

Joined: Aug 18, 2008
Posts: 30

Deepak Lal,

Here is the simplest java example:

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thanks for the example.



--
Deepak Lal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Queries
 
Similar Threads
Multithreading and concurrent user requests
descriptive link on how thread works.
Multithreaded Example... Where to put WAIT() , NOTIFY() advise please
multiple instances of JVM under Linux OS
What's your BEST learning resource on Java (multi)threading / Concurrency?