Jump to content

Executor.execute() - At the discretion of the Executor?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I have a question regarding the threads which Executor uses to run its Runnables. In the Javadoc for Executor, it says this:

Quote

void execute(Runnable command)

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.


My question is in regards to the bold green and red text above. What does it mean, "at the discretion of the Executor implementation?"

Basically, I want to ensure that my Executor does not execute my Runnable in the calling thread. The reason being is that the calling thread should not be tied up with lengthy tasks, as it needs to be free in the background to read and write data on a Socket. That's the whole point behind my using an Executor, so I can dump lengthy tasks into it whenever I want and forget about them (from the calling thread's point of view, that is). How can I be sure that the Executor won't use the calling thread?

I'm using a cached thread pool created by Executors.newCachedThreadPool() if that's relevant.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The newCachedThreadPool() method returns a ThreadPoolExecutor object.
The execute() method of the ThreadPoolExecutor object states that:

Quote

Executes the given task sometime in the future. The task may execute in a new thread or in an existing pooled thread. If the task cannot be submitted for execution, either because this executor has been shutdown or because its capacity has been reached, the task is handled by the current RejectedExecutionHandler.
So to me, it would seem that this implementation of Executor does NOT try running the command in the calling thread.

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Thanks for clearing that up for me.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'm not saying it's 100% correct, but that's how I would interpret the documentation! lol :)

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Sounds correct to me. I had no idea newCachedThreadPool() returns a ThreadPoolExecutor object. I only looked at the method summary list on the Executors javadoc page instead of the detailed method description.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
For those kind of details I prefer looking at the source itself, who knows they dare changing something from version 6.0.x to 6.0.y, or you look at the wrong javadoc version. (cause google mostly gives 1.4.2 -.- )
public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
(that's 6.0.26)

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

wim DC said:

For those kind of details I prefer looking at the source itself, who knows they dare changing something from version 6.0.x to 6.0.y, or you look at the wrong javadoc version. (cause google mostly gives 1.4.2 -.- )

public static ExecutorService newCachedThreadPool() {

        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

                                      60L, TimeUnit.SECONDS,

                                      new SynchronousQueue<Runnable>());

    }

(that's 6.0.26)

I believe it's the same in jdk 1.7.0

Like DC says, I also look in the src code for details like this.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users