*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instantiating a Thread page no 679 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instantiating a Thread page no 679" Watch "Instantiating a Thread page no 679" New topic
Author

Instantiating a Thread page no 679

sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
instantiating a thread implementing RUNNABLE interface is a bit confusing.
here they are saying to split the thread and job in 2 different classes,but in complete reference book they doing both things in the same class so i am getting confused.

it would be better if any one can explain me this with some coding.
thanks.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
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.


SCJP 5.0, SCWCD in progress
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
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.


thanks a lot for helping me.i will try the code you have mentioned.In future also do help me.please give me tips also for the SCJP exam.i have to clear it.
sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
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.


thanks a lot for helping me.i will try the code you have mentioned.In future also do help me.please give me tips also for the SCJP exam.i have to clear it.
sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
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.


thanks a lot for helping me.i will try the code you have mentioned.In future also do help me.please give me tips also for the SCJP exam.i have to clear it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating a Thread page no 679
 
Similar Threads
How To Lose a Job Via Facebook In 140 Characters or Less ?
9 1 1
WA #1.....word association
object of interface
Easy mock class intantiation