0

Hi I recently came across a blog which states:

Sometimes you may have to implement Runnable as well as subclass Thread. For instance, if creating a subclass of Thread that can execute more than one Runnable. This is typically the case when implementing a thread pool.

I am not sure what scenario this statement refers to.

Could you help me understanding such scenario?

Thanks.

SyntaX
  • 2,090
  • 17
  • 30
Abhijeet Kale
  • 1,656
  • 1
  • 16
  • 34
  • 1
    You would have to ask the author. I don't see any reason why you would have to subclass Thread. All you need is a Runnable that can call other Runnables. BTW, ThreadPoolExecutor, which is quite a complex and featured thread pool, takes a ThreadFactory as argument. So it doesn't have to create any Thread subclass. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#ThreadPoolExecutor%28int,%20int,%20long,%20java.util.concurrent.TimeUnit,%20java.util.concurrent.BlockingQueue,%20java.util.concurrent.ThreadFactory%29 – JB Nizet Nov 11 '15 at 08:19
  • I think you should ask the same question on the author's blog, if not mistaken [Jenkov](http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html) – SyntaX Nov 11 '15 at 08:23
  • I think the statement "thread that runs more than one Runnable" is misleading. A thread can only run the Runnable instance that is given by its constructor or it can run itself since a Thread is Runnable itself. The latter happens when you subclass Thread and override its run() method. – Serdar Nov 11 '15 at 08:25

3 Answers3

1

Check this link: "implements Runnable" vs. "extends Thread"

As Jon Skeet states:

In practical terms, it means you can implement Runnable and extend from another class as well.

Example:

public class Worker extends Person implements Runnable {...}

What you refer your quote is that you actually need use a Subclass of Thread for implementing more sophisticated Mechasisms. E.g. You implement a Thread which can work several tasks.

public SimpleExecutionService extends Thread() {

    private List<Runnable> myTasks;

    public SimpleExecutionService(List<Runnable> tasks){
        myTasks = tasks;
    }
    public void run() {
        for(Runnable task: myTasks) {
            task.run();
        }
    }

By the way, such kind of concurrency mechanisms exist plenty already.

Community
  • 1
  • 1
Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38
  • 1
    You don't actually need a Thread subclass to do that. Just make SimpleExecutionService implement Runnable instead of extending Thread, and pass a SimpleExecutionService instance to Thread's constructor, and you have the same result, without extending Thread. I agree that it's what the author probably meant to say, but he's wrong. – JB Nizet Nov 11 '15 at 10:34
1

I don't think the author implied that you would implement Runnable and subclass Thread in the same class. Naturally if you were implementing a thread pool from scratch you would have at least one worker implementation which would probably extend Thread, and you would have tasks which would probably implement Runnable. This would be a pretty basic and straightforward scenario.

SnakE
  • 2,355
  • 23
  • 31
  • Every time you extend Thread, you extend Thread and implement Runnable in the same class, since Thread already implements Runnable. There is no need to extend Thread to implement a worker. BTW, ThreadPoolExecutor does not. – JB Nizet Nov 11 '15 at 10:36
0

Actually Runnable interface is uesd when you want to extends another class in Thread implemented class. If you extends Thread class then there is no way to extends other important class because java doesn't support multiple inheritance na. So, elimaniting this problem, Runnable Thread Interface comes into picture and avoid this problem.....

Smartyy
  • 3
  • 1