Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Runnable interface  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!