[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. :wave:
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
Re: new Runnable(){...}.run()
Quote:
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?
Re: new Runnable(){...}.run()
Quote:
Originally Posted by oceanebelle
in what circumstances would the first code be considered?
Never
Quote:
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
Re: new Runnable(){...}.run()
I see thanks for the explanation! :D
I'll bear that in mind. However, I'll have to live with this code I guess.
Re: [RESOLVED] new Runnable(){...}.run()
And again, I'll have to spread the love before I can rate you ComputerJy. :sad: