Results 1 to 6 of 6

Thread: Threads

  1. #1
    Tanuck is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default Threads

    Hi, I want to ask whether it is necessary to call the Thread class constructor when extending it in your own class? For example...

    Java Code:
    public class TestThread extends Thread {
        
        public TestThread() {
            super();
            ....

    I have seen some example which do call super() and others which dont.

    Thanks

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    As far as I know, you need to call super() when extending Thread. However, another way to approach Threads (which is what I always use) is to implement it as Runnable instead.

    This link details reasons why you may prefer to use Runnable, and also explains the process of extending and creating a Thread (using either method): Java Threads Tutorial | Java Beginner

  3. #3
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    You don't need to call the parent class constructor.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    No, I don't think there is any reason to call super.

    Try it both ways and see if there is any difference.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    No, I don't think there is any reason to call super.

    Try it both ways and see if there is any difference.
    The first statement of the body of a ctor is either a call to any super class ctor or a call to another ctor in the same class (this). If there is no explicit call to a superclass ctor (and no call to another ctor in the same class) the compiler generates a no-args superclass ctor call: super(). If calls to other ctors in the same class cause a cycle the compiler detects it and emits an error message. If a class doesn't contain any ctors the compiler will generate an empty public no-args ctor.

    kind regards,

    Jos

  6. #6
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Exactly, i obviously agree with JosAH, all i want to add is that if you write super("ThreadName"), it will generate a thread whose name is ThreadName, but if you let it super(), it will genarate a thread whose name is Thread-k, where k is an integer greater than or equal to 0.

Similar Threads

  1. Threads
    By GoLuM83 in forum Advanced Java
    Replies: 7
    Last Post: 03-31-2010, 08:52 PM
  2. When to use threads
    By simorgh in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-12-2010, 07:43 AM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •