top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Java: Which way a developer should use for creating thread, i.e. Sub classing Thread or implementing Runnable.

+2 votes
90 views
Java: Which way a developer should use for creating thread, i.e. Sub classing Thread or implementing Runnable.
posted Dec 15, 2014 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

There are two ways of creating Thread in java

(i.e. sub classing or implementing Runnable).

It is very important to understand the implication of using these two approaches.

There are two different points about using these two approaches.

(1) By extending the thread class, the derived class itself is a thread object and it gains full control over the thread life cycle. Implementing the Runnable interface does not give developers any control over the thread itself, as it simply defines the unit of work that will be executed in a thread.

(2)Another important point is that when extending the Thread class, the derived class cannot extend any other base classes because Java only allows single inheritance. By implementing the Runnable interface, the class can still extend other base classes if necessary.

To summarize, if developer needs a full control over the Thread life cycle, sub classing Thread class is a good choice, and if programs need more flexibility by extending other class developer, should go with implementing Runnable interface.

answer Dec 16, 2014 by Karthick.c
Similar Questions
+1 vote

As JVM uses the primitive or time slicing scheduling techniques for thread scheduling. Is it possible to know our exiting JVM using which technique for thread scheduling?

+2 votes
import java.util.ArrayList;
import java.util.List;

public class ThreadB extends Thread {
    private static List<String> list1   = new ArrayList<String>();
    private static List<String> list2   = new ArrayList<String>();
    private static final Object mutex   = new Object();

    public void run() {
        try {
            sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (mutex) {
            list1.add("Hello");
            System.out.println("Added for 1   " + list1.size());
            list2.add("Added for 2");
            System.out.println("Added for 2   " + list2.size());
            mutex.notify();
        }
    }

    public static void main(String[] args) {
        ThreadB a1 = new ThreadB();
        ThreadB a2 = new ThreadB();
        a1.start();
        a2.start();
        new ThreadB().m1();    
    }

    private void m1() {         
        synchronized (list1) {
            System.out.println(list1.size());
            System.out.println(list2.size());
            try {
                list1.add("hello");
                System.out.println("m1  :::   " + list1.size());
                sleep(5000l);
                System.out.println("Oh mY God");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

Output is

0
0
m1  :::   1
Added for 1   2
Added for 2   1
Added for 1   3
Added for 2   2
Oh mY God

After locking list1, how other threads are going to access that list?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...