Wraps the given callable such that for the duration of Callable#call the thread that is running will have the given name. @param callable The callable to wrap @param nameSupplier The supplier of thread names, Supplier#get get will be called once for each invocation of the wrapp
(final Callable<T> callable, final Supplier<String> nameSupplier)
| 81 | */ |
| 82 | |
| 83 | @GwtIncompatible // threads |
| 84 | static <T> Callable<T> threadRenaming(final Callable<T> callable, final Supplier<String> nameSupplier) { |
| 85 | checkNotNull(nameSupplier); |
| 86 | checkNotNull(callable); |
| 87 | return new Callable<T>() { |
| 88 | @Override |
| 89 | public T call() throws Exception { |
| 90 | Thread currentThread = Thread.currentThread(); |
| 91 | String oldName = currentThread.getName(); |
| 92 | boolean restoreName = trySetName(nameSupplier.get(), currentThread); |
| 93 | try { |
| 94 | return callable.call(); |
| 95 | } finally { |
| 96 | if (restoreName) { |
| 97 | boolean unused = trySetName(oldName, currentThread); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Wraps the given runnable such that for the duration of {@link Runnable#run} the thread that is |
no test coverage detected