• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runnable interface

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what the code says here when it instantiates Runnable object.

public class GetPriority
{
private static Runnable makeRunnable()
{
Runnable r=new Runnable()
{
public void run()
{
for(int i=0;i<5;i++)
{
Thread t=Thread.currentThread();
S.o.p();

try
{
Thread.sleep(2000);
}catch(InterruptedException e)
{

}
}


}

};
return r;
}

public static void main(String args[])
{
Thread threadA=new Thread(makeRunnable(), "ThreadA");
threadA.start();


}
}

Can I implement the same program in a different way by implementing Runnable interface instead instantiating Runnable interface.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't directly instantiate an interface. The code, as posted, is incomplete, incorrect, or both.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Garrett, if you look at the code more carefully it is not instantiating Runnale interface as there is no semicolon after the Runnable r=new Runnable().
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are really not creating a Runnable. Let me show you show.

So let us look at the following code:



Okay the part that you have to understand is that the Runnable referance variable refers not to an instance of Runnable but to an instance of an anonymous class that implements Runnable. And because this ananoymous class is of type Runnable we still have to follow the rules of polymorhpism (hehe sorry for the spelling). So what does that mean in this situation? It means that we cannot do something like this:



We can only call the methods that that referance type has, which in this case is only run(). Here is one more example and this time I will expand a class and override a method.



So what happend here? The Foo referance refers to an anonymous subclass of Foo that override the fooStuff() method. So what will the output be here? "Override foo stuff"

Anyway read up on inner classes in Java and you will get a better understanding of what is going on.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sai donthneni:
Hi Garrett, if you look at the code more carefully it is not instantiating Runnale interface as there is no semicolon after the Runnable r=new Runnable().



You're right, sorry, the formatting threw mw off.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic