Home > Back-end >  Create multiple threads by extending Thread class that sharing same object in Java
Create multiple threads by extending Thread class that sharing same object in Java

Time:05-02

I was going through basics of multithreading and was writing a program to understand the difference between using the 2 approaches of creating threads.

I have read that using Runnable allows multiple threads to share same object and wanted to try similar thing while extending Thread. So after creating new object of Demo2 I passed the reference to Thread constructor (similar to what we do in Runnable).

I achieved what I was trying to as objT1, tT1, tT2 incremented value of sum to 3. But while printing out the current thread's name, it prints only demo2.1. I thought that the thread names that will be printed would be demo2.1, t1, t2 since I passed these names in constructor.

    class Main {
    public static void main(String args[]) {
        Demo1 objR1 = new Demo1();
        Demo2 objT1 = new Demo2("demo2.1");

        Thread tT1 = new Thread(objT1,"t1");
        Thread tT2 = new Thread(objT1,"t2");

        Thread tR1 = new Thread(objR1,"tR1");
        Thread tR2 = new Thread(objR1,"tR2");
    
        objT1.start();
        tT1.start();
        tT2.start();

        tR1.start();
        tR2.start();
    }
}


class Demo1 implements Runnable {

    int sum = 0;

    synchronized void calculate() {
        sum = sum  1;   
    }

    public void run()
    {
        calculate();    
        System.out.print(Thread.currentThread().getName()); 
        System.out.println(" " sum);
    }
}

class Demo2 extends Thread {

    int sum = 0;

    Demo2(String n) {
        super(n);   
    }
    
    synchronized void calculate() {
        sum = sum  1;       
    }

    public void run()
    {
        calculate();        
        System.out.println(this.getName() " " sum);
    }
}

Output:

demo2.1 1
demo2.1 2
demo2.1 3
tR1 1
tR2 2

So my question is - Does this snippet create 3 threads? If yes, then why aren't there 3 different names for each thread. If no, then what do these statements do.

Demo2 objT1 = new Demo2("demo2.1");
Thread tT1 = new Thread(objT1,"t1");
Thread tT2 = new Thread(objT1,"t2"); 

I know this must be something trivial but I cannot get answers in tutorials. Thanks in advance.

CodePudding user response:

Does this snippet create 3 threads?

Your program creates five threads. Each thread is responsible for one of the five lines of output that you showed.

I thought that the thread names that will be printed would be demo2.1, t1, t2 since I passed these names in constructor.

The five threads are named "demo2.1", "t1", "t2", "tR1", and "tR2", but the "t1" and "t2" threads never get to print their own names. That's because you gave each one of them a Runnable delegate, which happens to be the "demo2.1" thread instance.

The run() method of the "demo2.1" instance (the run() method of the Demo2 class) prints its own name, which is not always the name of the thread that is running it.

In more detail:

Here, you create a new object, which is a Demo2, and which is a Thread, and which implements Runnable, and which is named "demo2.1":

Demo2 objT1 = new Demo2("demo2.1")

When you call objT1.start(), the new thread calls the Demo2.run() method, and that method prints this.getName().

OK, That's simple, but the next bit is not so simple.

Here, you create another new object, which is a Thread that uses the previous objT1 as its Runnable delegate, and which is named "t1":

Thread tT1 = new Thread(objT1,"t1");

When you call tT1.start(), the new thread will call it's delegate's run() method. It will call Demo2.run(). But recall that Demo2 is a Thread with a name, and its run method prints its own name (actually, this.getName()) instead of printing the name of the thread that is running it (which would be Thread.currentThread().getName()).

Recap:

You are using objT1 in two different ways:

  1. You use it as a Thread
  2. You use it again, two more times, as the delegate of a different thread.

All three times, it prints its own name, which is not the same as the name of the thread that is running it in the two cases where you use the object as the delegate of a thread.

  • Related