Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Super keyword in java and multithreading

I am a newbie to the world of Java. While studying Multithreading I came across this program :-

class Shared 
{
 int x;
 synchronized void show(String s,int a)
{
  x=a;
  System.out.println("Starting in method" +s+  " " +x);
  try 
  {
     Thread.sleep(2000);
  }
  catch (Exception e)
  {
   System.out.println("Ending from method" +s+ " " +x);   
  }   
 }
}
class CustomThread extends Thread
{
  Shared s;
  public CustomThread(Shared s,String str)
{
 super (str);
  this.s=s;
  start();
}
 public void run()
{
 s.show(Thread.currentThread().getName(),10 );
}
}  

class CustomThread1 extends Thread
 {
    Shared s;
    public CustomThread1(Shared s,String str)
  {
     super (str);
  this.s=s;
  start();
}
   public void run()
  {
   s.show(Thread.currentThread().getName(),20);
  }
}   
class RunSyn
{
   public static void main(String...s)
   {
      Shared st=new Shared();
      CustomThread t1=new CustomThread(st,"one");
      CustomThread1 t2=new CustomThread1(st,"two");

       }
    }

What does the line
super(str);
does in this context?
As far as i understand it calls the constructor Thread (String name) of the Thread class. That in turn creates a new Thread object.
But in the output it is seen that the String passed in the constructor of CustomThread and CustomThread1 is being passed in the void show (String s,int a) of Shared class?
Please explain what is happening actually?
Thanks!!

like image 363
actuallyashish Avatar asked Jun 13 '26 10:06

actuallyashish


2 Answers

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.

The Thread class constructor that takes a String argument creates a Thread with a specific name (as opposed to automatically generated names such as Thread-1).

The easiest way for you to determine what the complete program does is to actually execute it.

When the start() method of a Thread instance is called the thread begins to execute (the run() method eventually gets called). As you can see, in this case, the run() method of CustomThread1 class simply calls the show(String, int) method of its Shared instance, which prints whatever it is passed into the console.

like image 62
Kallja Avatar answered Jun 15 '26 23:06

Kallja


As others have pointed out, the super(str) method calls the constructor for Thread. This overrides the default functionality which is to inject a super() call at the beginning of your constructor.

As for what it does, the string passed to Thread will give the thread a name. This will allow you to easily identify the thread in debugging sessions and in Thread dumps. Without the string, it will have a boring name like, "Thread-5".

like image 27
64BitBob Avatar answered Jun 15 '26 22:06

64BitBob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!