all events occur in real time
SCJP 5.0, SCWCD in progress
SCJP 5.0
Originally posted by Kelvin Lim:
Hi Sapana,
Both are valid ways of achieving the same end result, so there's no clear-cut right or wrong here. However, you should know that it's always the Thread class that does the real work of starting a new thread of execution. When you call a Thread object's start() method, the Thread object will create a new thread and always execute its own run() method. But here's how the Runnable comes into the picture: if you had passed the Thread constructor a Runnable object, Thread's run() method will then in turn call the associated Runnable's run() method.
It's probably easier to understand this by studying a sample implementation of the Thread class. Here are the key sections of Thread's code relevant to this discussion, adapted slightly from the open-source GNU Classpath implementation of Thread:
Why would a programmer ever want to use a separate Runnable job class instead of just extending Thread and overridding its run() method? The most common reason is this: Java allows each class to extend only one other class, but your program design requires that your job class inherit from a different class. So you can't extend Thread... but you can implement as many interfaces as you want. Hence the designers of Java provided this mechanism where you can just implement Runnable and use the Thread(Runnable) constructor to execute your job.
Originally posted by Burkhard Hassel:
Howdy Sapana!
Welcome to the ranch!
![]()
Simple example, the runnable is one class and the threads are created in the main method of another class:
Output:
Current thread: Thread[Thread-0,5,main]
runs Runner@1e63e3d
Current thread: Thread[Thread-1,5,main]
runs Runner@1e63e3d
Current thread: Thread[Thread-2,5,main]
runs Runner@1e63e3d
Current thread: Thread[Thread-3,5,main]
runs Runner@1e63e3d
Current thread: Thread[Thread-4,5,main]
runs Runner@1e63e3d
There are five threads, but they use all the same runnable object.
Yours,
Bu.
Originally posted by Mohit Jain:
Hi
In order to create a thread we always need an object of java.lang.Thread.
While creating this Thread object we call a Thread constructor that accepts a Runnable object. When we start this thread using start(), it calls the run() method of java.lang.Thread class which further passes the call to the run() method of current Runnable object. (Look into jdk source code for "Thread.java" and it will be understood)
For example -
class X implements Runnable
{
Thread t;
X()
{
t = new Thread(this);
t.start();
}
public void run() {}
...
}
By passing "this" we specify the object whose run() method will be ultimately executed.
If we split classes and specify run() method in a different class, we have to pass that class object instead of "this" as follows -
class X
{
Thread t;
X()
{
t = new Thread(new Y());
t.start();
}
}
class Y implements Runnable
{
public void run() {}
..
}
Hope this answers your question.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |