0

I got a class:

public class RunnableImplA implements Runnable {


    private Runnable runnableB;
    
    public RunnableImplA(Runnable runnableB) {
        super();
        runnableB= runnableB;
    }


    @Override
    public void run() {
        try {
        
            runnableB.run();
        } finally {
            // some code
        }
    }
}

Do RunnableImplA & runnableB run on same Thread?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47

3 Answers3

3

Runnable.run() does NOT create a thread. It's just a simple function call, nothing special happens. So, yes, same thread.

kutschkem
  • 7,826
  • 3
  • 21
  • 56
2

Yes, it's running in the same thread.

A Runnable is just an interface, nothing else. Some classes like Thread can accept the Runnable as an argument.

stackbiz
  • 1,136
  • 1
  • 5
  • 22
0

In order to create new Thread you need to call start() method of Thread class, which internally calls the run() method. As here we don't call it with start method, it is just like any other method call on object. So yes, it will be in same Thread.

If you want to run it in different thread, then run() method needs to be updated as below:

@Override
    public void run() {
        try {
            Thread innerThread = new Thread(runnableB);
            innerThread.start();
        } finally {
            // some code
        }
    }
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47