What is the target object in java thread?
By zxh606110
@zxh606110 (32)
China
October 29, 2007 7:31am CST
i'm learning java Thread nowdays,and now i met some questions,such as the target object,i can't understand it well,is there any programmer can explain for me in detial?Thanks!
1 response
@luxram2330 (76)
• India
15 Nov 07
An Object that wants to serve as the target of a Thread can declare that it has an appropriate executable method by implementing the java.lang.Runnable interface. Runnable defines a single, general-purpose method:
public interface Runnable {
abstract public void run();
}
Every thread begins its life by executing a run() method in a particular object. run() is a rather mundane method that can hold an arbitrary body of code. It is public, takes no arguments, has no return value, and is not allowed to throw any exceptions.
Any class can contain an appropriate run() method, simply by declaring that it implements the Runnable interface. An instance of this class is then a runnable object that can serve as the target of a new thread. In this way, we can effectively run a method in any object we want.