• 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

Threads

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it valid to speciy the run method for a class that extends Thread not in that class but in a sub-class of that class?
An example in Mughal's book shows the run() method in the sub-class but the start() is in the super-class that extends Thread.
It works but don't understand the concept??
Thnx.
[ August 10, 2002: Message edited by: Dinesh Kumar ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write an example program, run it. If it does not work post it here.
-Barry
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote it
------------------------------------------------
public class ThreadTest extends Thread
{
public void run()
{
System.out.println("ThreadTest run");
}
public void startit(){start();}
public static void main(String args[])
{
ThreadTest t=new Thread1();
t.startit();
}
}
class Thread1 extends ThreadTest
{
public void run()
{
System.out.println("thread1 run");
}
};
-------------------------------------------------
I wondered in which condition can we use this form of start the thread.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the purpose of the author was just to make the readers aware of this possibility regarding the exam.
Otherwise it seems not very useful.
Your question can be rephrased as why we extend?
We make a class derived of a base one when we want to customize the base class. Generally by adding more fields and/or adding/overriding methods.
In the example, to get a different run, it is possible just to create another class extending directly from Thread.
Extending from a class that extends from Thread is not very useful. A class that extends from Thread is mainly intended to be used as a Thread. It could implement an interface and be used as that type; however this design is awkward. Now to customize just a Thread it is not needed to derived from a class that already derives from Thread.
Please use UBB code to ident the posts
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Kumar:
It is possible that authur is trying to convey
the concept of polymorphism:

This statement expresses that t is of type ThreadTest
but currently holding an object of type Thread1,
whereas Thread1 is a type of ThreadTest. So when
a method on t is called, the corresponding
overriding method in Thread1 will be executed.
It is possible that at run time, t holds another
type Thread2 rather than Thread1. In that case
overriding method in Thread2 will be called.
That is the benefit of polymorphism. The method
binding is done at run time rather than compile
time.
I hope this helps..
Thanks
Barkat
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It'very useful!
Think about virtual function in C++,
that just the key feature of OOP, this
is just the same.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HiBob,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explaining polymorphism using threads? That doesn't seem a good separation of concerns.
 
HiBob Chu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not explaining polymorphism using threads,
it's apply polymorphism in threads.
we can imaging:
There are such classes: Rectangle(Thread's subclass), Square( Rectangle's subclass), GoldenSquare(Square's subclass),
GoldenAndTwinkleSquare(GoldenSquare'sub class).
Every class's run function acts different from
eachother. But in a program, we didn't know which
type of object will be created and start to run untill the program run. Using polymorphism, we can call the right run function in runtime.
 
HiBob Chu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mughal writed this in his book. Maybe he
want to note people , the run function is
not only need to be ovrriden but also can
be use agility.
reply
    Bookmark Topic Watch Topic
  • New Topic