Threads Beginner: What is the use of the super () method in the constructor of a helper class (extension of the Thread class)?

The super () function is used to execute the superclass constructor from within the subclass constructor. In this program, the output remains unchanged with or without super () inside the constructor. Could you explain the difference between the presence and absence of 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); } } } 
+5
source share
3 answers

By Thread.currentThread().getName() superclass constructor Thread(String name) you explicitly specify the name of the system thread, which you can later get with Thread.currentThread().getName() . Unless you explicitly call the constructor of the superclass, a default constructor will be used, which will assign a default thread name, for example Thread-1 . These thread names can be displayed in the debugger, jstack , etc. interface, so it is recommended that you assign meaningful names to your threads. For example, when you run your current code in the Eclipse debugger, you will see:

Topic Title

So you can see Thread [Some Child Thread] . But if you delete this super call, you will see the following:

No thread name

Now this is just Thread [Thread-0] .

+5
source

If you make the call super, as you did, you will end up calling the following constructor according to the JavaDoc:

public Thread (string name)

Selects 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 delete your call to super(ThreadName) , then the JVM will automatically call the empty super() constructor for you. According to the JavaDoc, you will get an almost identical call:

Thread Public Thread () Selects a new Thread object. This constructor has the same effect as

Thread (null, null, gname) , where gname is the new 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 the automatically generated name for Thread.

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

+2
source

super(ThreadName); operator super(ThreadName); calls the Thread(String name) constructor of the Thread(String name) class. As follows from the argument, it sets the name to thread .

When setting the thread value, the name may not be directly useful in this example; in general, the thread name may be useful in debugging, since you can add thread names to application logs.

+1
source

All Articles