|
-
Sep 25th, 2008, 04:44 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] new Runnable(){...}.run()
Code 1:
Code:
new Runnable(){
public void run() {
assert(false);
}
}.run();
Code 2:
Code:
new Thread(new Runnable(){
public void run() {
assert(false);
}
}).start();
Apart from the obvious differences, what are their differences and what is the advantage or disadvantage of each?
Need Advice.
-
Sep 25th, 2008, 06:47 AM
#2
Re: new Runnable(){...}.run()
The 1st use of the Runnable interface is meaningless, because you're creating an instance then calling it's only method leaving a memory load for the garbage collector.
The 2nd statement is a correct use of the Runnable interface. Creating a Runnable instance to run on a different thread
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 25th, 2008, 08:52 AM
#3
Thread Starter
Frenzied Member
Re: new Runnable(){...}.run()
 Originally Posted by ComputerJy
The 1st use of the Runnable interface is meaningless, because you're creating an instance then calling it's only method leaving a memory load for the garbage collector.
I don't understand that bit about the memory load and I have never seen code done this way prior to messing around with a source code. So it got me wondering that there might be some advantages to doing execution this way.
Could you elaborate on the memory load thing? And in what circumstances would the first code be considered?
-
Sep 25th, 2008, 12:40 PM
#4
Re: new Runnable(){...}.run()
 Originally Posted by oceanebelle
in what circumstances would the first code be considered?
Never
 Originally Posted by oceanebelle
Could you elaborate on the memory load thing?
Every time your code gets executed, A new Runnable Object will be instantiated, then the method run will be invoked. which will lead to:
Unnecessary instructions and An object in memory with no references to it awaiting garbage collection...
So there are no advantages
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 26th, 2008, 03:32 AM
#5
Thread Starter
Frenzied Member
Re: new Runnable(){...}.run()
I see thanks for the explanation! 
I'll bear that in mind. However, I'll have to live with this code I guess.
-
Sep 26th, 2008, 03:34 AM
#6
Thread Starter
Frenzied Member
Re: [RESOLVED] new Runnable(){...}.run()
And again, I'll have to spread the love before I can rate you ComputerJy. :sad:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|