jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Runnable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runnable interface " Watch "Runnable interface " New topic
Author

Runnable interface

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
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.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can't directly instantiate an interface. The code, as posted, is incomplete, incorrect, or both.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
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().
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
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.


Jerret Halter<br /> <br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.<hr></blockquote>
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Runnable interface
 
Similar Threads
thread
regarding threads
Insights into synchronization
Joining ThreadB to ThreadA
Multiple Threads Accessing Multiple Methods Independently