• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instantiating a Thread page no 679

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic