Friday, January 25, 2019

multithreading - Java-Thread Vs Runnable

While reading through the significant difference between Thread and Runnable from here, I encountered a difference that is:



When you extends Thread class, each of your thread creates unique object and associate with it. where as



When you implement Runnable, it shares the same object to multiple threads..



There is code give :




class ImplementsRunnable implements Runnable {

private int counter = 0;

public void run() {
counter++;
System.out.println("ImplementsRunnable : Counter : " + counter);
}
}


class ExtendsThread extends Thread {

private int counter = 0;

public void run() {
counter++;
System.out.println("ExtendsThread : Counter : " + counter);
}
}


public class ThreadVsRunnable {

public static void main(String args[]) throws Exception {
//Multiple threads share the same object.
ImplementsRunnable rc = new ImplementsRunnable();
Thread t1 = new Thread(rc);
t1.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
Thread t2 = new Thread(rc);
t2.start();

Thread.sleep(1000); // Waiting for 1 second before starting next thread
Thread t3 = new Thread(rc);
t3.start();

//Creating new instance for every thread access.
ExtendsThread tc1 = new ExtendsThread();
tc1.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
ExtendsThread tc2 = new ExtendsThread();
tc2.start();

Thread.sleep(1000); // Waiting for 1 second before starting next thread
ExtendsThread tc3 = new ExtendsThread();
tc3.start();
}
}


The output is something like this:



ImplementsRunnable : Counter : 1

ImplementsRunnable : Counter : 2
ImplementsRunnable : Counter : 3
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1


It proves differences given above. I make a slight modification in code given below:



class ImplementsRunnable implements Runnable {


private int counter = 0;

public void run() {
counter++;
System.out.println("ImplementsRunnable : Counter : " + counter);
}
}

class ExtendsThread extends Thread {


private int counter = 0;

public void run() {
counter++;
System.out.println("ExtendsThread : Counter : " + counter);
}
}

public class ThreadVsRunnable {


public static void main(String args[]) throws Exception {
//Multiple threads share the same object.
ImplementsRunnable rc = new ImplementsRunnable();
Thread t1 = new Thread(rc);
t1.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
Thread t2 = new Thread(rc);
t2.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread

Thread t3 = new Thread(rc);
t3.start();

//Modification done here. Only one object is shered by multiple threads here also.
ExtendsThread extendsThread = new ExtendsThread();
Thread thread11 = new Thread(extendsThread);
thread11.start();
Thread.sleep(1000);
Thread thread12 = new Thread(extendsThread);
thread12.start();

Thread.sleep(1000);
Thread thread13 = new Thread(extendsThread);
thread13.start();
Thread.sleep(1000);
}
}


Now the output is :




ImplementsRunnable : Counter : 1
ImplementsRunnable : Counter : 2
ImplementsRunnable : Counter : 3
ExtendsThread : Counter : 1
ExtendsThread : Counter : 2
ExtendsThread : Counter : 3


I understand the fact that, here same object (extendsThread) is shared by three threads. But I'm confused here that how it is different from implementing Runnable. Here, even if *ExtendsThread * extends Thread, we are still able to share the object of this class to other threads. In my thinking the above difference does not make any sense.




Thanks.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...