This discussion is archived
5 Replies Latest reply: Feb 14, 2013 1:27 PM by Murray9654 RSS

how can one thread execute multiple runnables?

Murray9654 Newbie
Currently Being Moderated
Hi the following is from the 1.6 api docs
Executors.newSingleThreadExecutor() 
          Creates an Executor that uses a single worker thread operating off an unbounded queue.
Normally we have to supply the Runnable when the thread is constructed itself and once the thread finish executing its run method the thread is dead and cannot be started again as a thread.

But the above doc says that one single worker thread is enough to execute queue of Runnables. how this possible?
  • 1. Re: how can one thread execute multiple runnables?
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    But the above doc says that one single worker thread is enough to execute queue of Runnables. how this possible?
    Nothing stops you from creating a Thread that has a list of Runnables and calls their run() method one after another.
    new Thread(new Runnable() {
        public void run() {
            for(Runnable r : listOfRunnables)
                r.run();
            }
        }
    }).start();
    Basic threading stuff.
  • 2. Re: how can one thread execute multiple runnables?
    Murray9654 Newbie
    Currently Being Moderated
    Kayaman wrote:
    Muralidhar wrote:
    But the above doc says that one single worker thread is enough to execute queue of Runnables. how this possible?
    Nothing stops you from creating a Thread that has a list of Runnables and calls their run() method one after another.
    new Thread(new Runnable() {
    public void run() {
    for(Runnable r : listOfRunnables)
    r.run();
    }
    }
    }).start();
    Basic threading stuff.
    Point less. to that matter we don't even need to provide a Runnable. it could be any classs. we can write our own class, put them in the list and can call them inside the run method as shown above.

    I have gone through the source code and found that, at any given point of time there will be only one worker thread running the unbounded queue of Runnables. As the Runnables comes in they will be queued and one Runnable will be executed by one Fresh new thread and the thread dies once execution is complete. So if there are 4 Runnables in queue, 4 threads runs them (each thread one runnable) but it happen in a sequence(one thread after the another) so at any given point of time in that ExecutorService there will be only one single worker thread running.

    Edited by: Muralidhar on Feb 15, 2013 12:18 AM

    Edited by: Muralidhar on Feb 15, 2013 12:19 AM
  • 3. Re: how can one thread execute multiple runnables?
    Murray9654 Newbie
    Currently Being Moderated
    self answered
  • 4. Re: how can one thread execute multiple runnables?
    jtahlborn Expert
    Currently Being Moderated
    Muralidhar wrote:
    Kayaman wrote:
    Muralidhar wrote:
    But the above doc says that one single worker thread is enough to execute queue of Runnables. how this possible?
    Nothing stops you from creating a Thread that has a list of Runnables and calls their run() method one after another.
    new Thread(new Runnable() {
    public void run() {
    for(Runnable r : listOfRunnables)
    r.run();
    }
    }
    }).start();
    Basic threading stuff.
    Point less. to that matter we don't even need to provide a Runnable. it could be any classs. we can write our own class, put them in the list and can call them inside the run method as shown above.

    I have gone through the source code and found that, at any given point of time there will be only one worker thread running the unbounded queue of Runnables. As the Runnables comes in they will be queued and one Runnable will be executed by one Fresh new thread and the thread dies once execution is complete. So if there are 4 Runnables in queue, 4 threads runs them (each thread one runnable) but it happen in a sequence(one thread after the another) so at any given point of time in that ExecutorService there will be only one single worker thread running.
    actually, that is incorrect. a single threaded executor uses one thread which lives indefinitely. the only time you would see a new thread is if the current thread dies abnormally. you can easily test this by queing up a bunch of rnunables which all print out the current thread id. Kayaman's example was showing you how the single threaded executor is effectively implemented.
  • 5. Re: how can one thread execute multiple runnables?
    Murray9654 Newbie
    Currently Being Moderated
    Ya you are right. while checking the source code I have missed something so had a keen look again and found it is the same thread running all the queue. It is the same as Kayamans example. But to this matter why do we need Runnables. It could be any class with a xx method.

    Ok any way thanks to you and Kayaman.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points