Question

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!!

Was it helpful?

Solution

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.

OTHER TIPS

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".

Your constructor calls two methods: super(str) and start(). The first method is the constructor of the superclass, so Thread(String) which creates a new Thread object with the given string as name.

The second method, start(), actually creates and starts a new thread. In this new thread, the run() method is called (similar to main() of the entire program, but for a thread). This method then calls s.show(Thread.currentThread().getName(),10);. Since you set the thread name to the given string str, Thread.currentThread().getName() returns that same string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top