-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add utility function to check if an executor is direct or not #143
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
package com.spotify.folsom.client; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
|
@@ -72,4 +74,43 @@ static <T> CompletionStage<T> onExecutor(CompletionStage<T> future, Executor exe | |
} | ||
return future.whenCompleteAsync((t, throwable) -> {}, executor); | ||
} | ||
|
||
/** | ||
* Checks if an executor is direct or not. Direct means that the executor executes work on the | ||
* same thread as the work was submitted on. | ||
* | ||
* @param executor may not be null | ||
* @return true if the executor is a direct executor, otherwise false | ||
*/ | ||
static boolean isDirectExecutor(Executor executor) { | ||
requireNonNull(executor); | ||
RunnableWithThread runnableWithThread = new RunnableWithThread(); | ||
try { | ||
executor.execute(runnableWithThread); | ||
|
||
// We have the following cases | ||
// 1) It is a direct executor, so runnableWithThread.thread will be set to the current thread. | ||
// We will correctly return true | ||
// 2) It is not a direct executor and runnableWithThread.thread is still null when we check | ||
// it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicking: wrong indentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops |
||
// We correctly return false | ||
// 3) It is not a direct executor but the runnableWithThread.thread somehow managed to get set | ||
// before we check it. In any case, it can't be referencing the same thread we are in | ||
// We correctly return false | ||
|
||
return runnableWithThread.thread == Thread.currentThread(); | ||
} catch (Exception e) { | ||
// If the executor throws any exception, it can not be a direct executor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this as safe assumption? Looking at the Guava DirectExecutorService, it can throw exceptions here, but only if the executor is shutting down, which is probably fine either way. But, would we want to assume other direct executors would behave the same way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, probably not a safe assumption. It depends a bit on what the function is used for. If it's only for optimization purposes and sanity checks it may still be good enough |
||
return false; | ||
} | ||
} | ||
|
||
private static class RunnableWithThread implements Runnable { | ||
private Thread thread; | ||
|
||
@Override | ||
public void run() { | ||
thread = Thread.currentThread(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking here, but given isDirectExecutor really meaning "is a direct executor or null", how about rather naming it something like "wrapWithExecutor"? (I'm terrible at naming so I'm sure an even better name would be available)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that's a good idea