Results 1 to 12 of 12
- 05-06-2009, 10:56 AM #1
- 05-06-2009, 11:39 AM #2
As far as my knowledgee is concerned Thread using Runnable is the best one.
Because u can extend other class.
Example : AppletsRamya:cool:
- 05-06-2009, 12:31 PM #3
Thanks Ramya for sharing your thoughts with me.
But my friend told that whenever we use divide and conquer in our application,Extends Thread class is more comfortable than previous one.
Do you have any idea about it?Are you indian?Mak
(Living @ Virtual World)
- 05-06-2009, 12:34 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Neither is intrinsically "better"-- which to use largely depends on what seems a more logical design for your program.
In principle, a Runnable represents what gets run, while a Thread represents how it gets run: the Thread object is where you can set things like name and thread priority, and is where you actually start the task running.
There are also other ways of running a Runnable without explicitly wrapping a thread around it: for example, the Java 5 concurrency utilities such as ThreadPoolExecutor, or SwingUtilities.invokeLater().
However, as a "shortcut", you can override the run() method on Thread. This is convenient where (a) your runnable class doesn't need to override anything else, and (b) it makes sense for you to keep the "what" code next to/coupled with the "how" code. Whether (a) and (b) are true really depends on your particular program.
Some more info I've written: constructing Threads and Runnables.Last edited by neilcoffey; 05-06-2009 at 12:37 PM. Reason: added extra examples/refs
Neil Coffey
Javamex - Java tutorials and performance info
- 05-06-2009, 12:47 PM #5
Hi,
I am also in favour of implementing Runnable interface instead of extending Thread class.Sharing knowledge means gaining more knowledge.:)
- 05-26-2009, 03:43 AM #6
While I agree with what Neil said, I think that the focus should typically be one what is running, not how. The nice thing about implementing Runnable is that the object does not *have* to be run on a thread. You gain flexibility without losing anything.
- 05-27-2009, 06:18 AM #7
I thought I read in the api not to extend threads, and finally found that passage:
from java.lang.RunnableIn most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-28-2009, 04:26 AM #8
The JavaDoc has spoken. Word. ;-)
In this case, AB is correct in quoting the API. I just hear that at work all the time, generally about things where what Sun says really doesn't have anything to do with the problem at hand...
- 05-30-2009, 07:24 AM #9
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Implementing runnable is 'better' in the sense of flexibility, but quite often, you don't care about that flexibility and just want 1 simple thing done in a different thread. In a case like that, extending Thread via an anonymous class often provides the most concise and maintainable code.
It all depends on what is meant by 'better'.
- 05-30-2009, 10:24 AM #10
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Only extend the Thread class if you want to extend the function ability of the Thread... hence the name, extend...
I die a little on the inside...
Every time I get shot.
- 06-07-2009, 03:06 AM #11
Member
- Join Date
- Apr 2008
- Posts
- 21
- Rep Power
- 0
it's simply: Runnable are good for visual modules of app -- Thread are good for calculation's tasks.
- 06-08-2009, 08:00 AM #12
Similar Threads
-
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 01:21 PM -
multiple threads access the same method.
By bhanu in forum New To JavaReplies: 3Last Post: 02-16-2009, 06:54 AM -
Creating a GUI - which method
By matpj in forum New To JavaReplies: 3Last Post: 01-15-2009, 04:40 PM -
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09-21-2008, 05:32 PM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks