Repository: square/retrofit
Issue: Clear Thread Name When Idle

Reset the thread name to "Retrofit-Idle" when the background operations complete.


Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/retrofit/src/main/java/retrofit/http/Platform.java b/retrofit/src/main/java/retrofit/http/Platform.java
index da9bfc7fee..1a8de06d21 100644
--- a/retrofit/src/main/java/retrofit/http/Platform.java
+++ b/retrofit/src/main/java/retrofit/http/Platform.java
@@ -7,7 +7,6 @@
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.atomic.AtomicInteger;
 import retrofit.http.android.AndroidApacheClient;
 import retrofit.http.android.MainThreadExecutor;
 import retrofit.http.client.Client;
@@ -15,7 +14,7 @@
 
 import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
 import static java.lang.Thread.MIN_PRIORITY;
-import static retrofit.http.RestAdapter.THREAD_PREFIX;
+import static retrofit.http.RestAdapter.IDLE_THREAD_NAME;
 import static retrofit.http.Utils.SynchronousExecutor;
 
 abstract class Platform {
@@ -55,15 +54,13 @@ private static class Base extends Platform {
 
     @Override Executor defaultHttpExecutor() {
       return Executors.newCachedThreadPool(new ThreadFactory() {
-        private final AtomicInteger threadCounter = new AtomicInteger();
-
         @Override public Thread newThread(final Runnable r) {
           return new Thread(new Runnable() {
             @Override public void run() {
               Thread.currentThread().setPriority(MIN_PRIORITY);
               r.run();
             }
-          }, THREAD_PREFIX + threadCounter.getAndIncrement());
+          }, IDLE_THREAD_NAME);
         }
       });
     }
@@ -99,15 +96,13 @@ private static class Android extends Platform {
 
     @Override Executor defaultHttpExecutor() {
       return Executors.newCachedThreadPool(new ThreadFactory() {
-        private final AtomicInteger threadCounter = new AtomicInteger();
-
         @Override public Thread newThread(final Runnable r) {
           return new Thread(new Runnable() {
             @Override public void run() {
               Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
               r.run();
             }
-          }, THREAD_PREFIX + threadCounter.getAndIncrement());
+          }, IDLE_THREAD_NAME);
         }
       });
     }
diff --git a/retrofit/src/main/java/retrofit/http/RestAdapter.java b/retrofit/src/main/java/retrofit/http/RestAdapter.java
index 1e209138a5..7c67c2d524 100644
--- a/retrofit/src/main/java/retrofit/http/RestAdapter.java
+++ b/retrofit/src/main/java/retrofit/http/RestAdapter.java
@@ -31,6 +31,7 @@
 public class RestAdapter {
   private static final int LOG_CHUNK_SIZE = 4000;
   static final String THREAD_PREFIX = "Retrofit-";
+  static final String IDLE_THREAD_NAME = THREAD_PREFIX + "Idle";
 
   /** Simple logging abstraction for debug messages. */
   public interface Log {
@@ -234,6 +235,10 @@ private Object invokeRequest(RestMethodInfo methodDetails, Object[] args) {
         throw RetrofitError.networkError(url, e);
       } catch (Throwable t) {
         throw RetrofitError.unexpectedError(url, t);
+      } finally {
+        if (!methodDetails.isSynchronous) {
+          Thread.currentThread().setName(IDLE_THREAD_NAME);
+        }
       }
     }
   }


Test patch: