Explicit call of Runnable.run

Posted by klaudio on Stack Overflow See other posts from Stack Overflow or by klaudio
Published on 2010-04-19T06:35:39Z Indexed on 2010/04/19 6:43 UTC
Read the original article Hit count: 240

Filed under:
|

Hi, I have a question.

Somebody, who was working on my code before me, created some method and passed Runnable as parameter, more likely:

void myMethod(Runnable runnable){ runnable.run(); }

Then calling myMethod out of main looks like:

public static void main(String args[]) 
{ try 
{ myMethod(new Runnable(){ public void run() { //do something...; }}); } 
catch (Throwable t) { } }

So, to supply parameter to myMethod I need to instantiate object of (in this case anonymous) class implementing Runnable.

My question is: is it necessary to use Runnable in this example? Can I use any different interface? I mean I can create new interface with single method i.e.

interface MyInterface{ void doThis(); }

then change look of myMethod: void myMethod(MyInterface myObject){ myObject.doThis(); }

And of course client too:

public static void main(String args[]) { 
try { myMethod(new MyInterface (){ public void doThis() 
{ //do something...; }}); } 
catch (Throwable t) { } }

Or maybe something is about Runnable?!

© Stack Overflow or respective owner

Related posts about multithreading

Related posts about interface