Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads Beginner: What's the point in using a super() method within the sub class's(extending Thread class) constructor?

A super() is used to execute super class's constructor from within sub class's constructor. In this program, the output remains the same with or without the super() inside the constructor. So could you please explain the difference between having and not having super() in this scenario?

    class SomeThread extends Thread {
    String ThreadName;

    SomeThread(String ThreadName) {
        super(ThreadName); //Why Oh Why ?
        this.ThreadName = ThreadName;
    }

    public void run(){
        for(int ctr=1; ctr<=10; ctr++) {
            System.out.println("From "+ThreadName+"...."+ctr);
            try {
                sleep(1000);
            }catch(Exception e) {
                System.out.println("Exception in "+ThreadName);
            }
        }
    }

}

class ThreadAliveDemo {
    public static void main(String[] args) throws Exception {
        SomeThread FThread = new SomeThread("Some Child Thread");
        System.out.println("Some Child Thread is alive "+FThread.isAlive());

        FThread.start();

        System.out.println("Some Child Thread is running.."+FThread.isAlive());

        for(int ctr = 1; ctr<=5; ctr++) {
        System.out.println("From.."+Thread.currentThread().getName()+"..."+ctr);
        Thread.sleep(1000);
        }

        FThread.join();

        System.out.println("Some Child Thread is alive.."+FThread.isAlive());

        for(int ctr=6; ctr<=10; ctr++) {
        System.out.println("From.."+Thread.currentThread().getName()+"..."+ctr);
        Thread.sleep(1000);
        }
    }
}
like image 832
tardycoder30 Avatar asked Sep 20 '15 11:09

tardycoder30


People also ask

Why does the super () statement is used as the first statement of child constructor?

Why? The Sun compiler says, call to super must be first statement in constructor . The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() .

Where super () can be used within a constructor?

super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.

What is super class of thread in Java?

The super(String) statement is an explicit super class constructor call. All java constructors must call one or another constructor from their super classes. By default the no-args constructor is invoked, but in this way you can explicitly specify which super class constructor to invoke.

When a class is extends thread class which method it should override?

Java static code analysis: Classes extending java. lang. Thread should override the "run" method.


3 Answers

Calling the superclass constructor Thread(String name) you explicitly set the system thread name which you can later get with Thread.currentThread().getName(). If you don't call the superclass constructor explicitly, a default constructor will be used which will assign the default thread name like Thread-1. Those thread names can appear in debugger UI, jstack output and so on, so it's a good idea to assign meaningful names to your threads. For example, when running your current code in Eclipse debugger you'll see:

Thread name

So you can see Thread [Some Child Thread]. But if you remove that super call, you'll see the following:

No thread name

Now it's just Thread [Thread-0].

like image 91
Tagir Valeev Avatar answered Oct 15 '22 03:10

Tagir Valeev


If you do make the call to super as you have done, then you will end up calling the following constructor according to the JavaDoc:

public Thread(String name)

Allocates a new Thread object. This constructor has the same effect as Thread (null, null, name).

So super(ThreadName) calls super(null, null, ThreadName).

If you remove your call to super(ThreadName), then the JVM will call the empty constructor super() automatically for you. According to the JavaDoc you will end up making an almost identical call:

Thread public Thread() Allocates a new Thread object. This constructor has the same effect as

Thread (null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

So super() calls super(null, null, gname), where gname is an auto-generated name for the Thread.

The only difference in behavior between calling and not calling super() is the assignment of a Thread name.

like image 23
Tim Biegeleisen Avatar answered Oct 15 '22 02:10

Tim Biegeleisen


The super(ThreadName); statement invokes the Thread(String name) constructor of Thread class. As the argument suggests, it sets the name of the thread.

While setting thread's name may not be directly useful in this example, in general, thread name can be useful when debugging, since you can add thread names to your application's logs.

like image 30
Amila Avatar answered Oct 15 '22 03:10

Amila