Skip to content

Commit 8320517

Browse files
committed
Not starting many threads to cancel requests, just one android-async-http#533
1 parent 2b3a062 commit 8320517

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.loopj.android.http;
2020

2121
import android.content.Context;
22+
import android.os.Looper;
2223
import android.util.Log;
2324

2425
import org.apache.http.Header;
@@ -339,8 +340,8 @@ public void setThreadPool(ThreadPoolExecutor threadPool) {
339340
}
340341

341342
/**
342-
* Returns the current executor service used. By default,
343-
* Executors.newFixedThreadPool() is used.
343+
* Returns the current executor service used. By default, Executors.newFixedThreadPool() is
344+
* used.
344345
*
345346
* @return current executor service used
346347
*/
@@ -353,7 +354,7 @@ public ExecutorService getThreadPool() {
353354
*
354355
* @return The default threading pool to be used
355356
*/
356-
protected ExecutorService getDefaultThreadPool() {
357+
protected ExecutorService getDefaultThreadPool() {
357358
return Executors.newCachedThreadPool();
358359
}
359360

@@ -578,13 +579,27 @@ public void clearBasicAuth() {
578579
* @param mayInterruptIfRunning specifies if active requests should be cancelled along with
579580
* pending requests.
580581
*/
581-
public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
582-
List<RequestHandle> requestList = requestMap.get(context);
583-
if (requestList != null) {
584-
for (RequestHandle requestHandle : requestList) {
585-
requestHandle.cancel(mayInterruptIfRunning);
582+
public void cancelRequests(final Context context, final boolean mayInterruptIfRunning) {
583+
if (context == null) {
584+
Log.e(LOG_TAG, "Passed null Context to cancelRequests");
585+
return;
586+
}
587+
Runnable r = new Runnable() {
588+
@Override
589+
public void run() {
590+
List<RequestHandle> requestList = requestMap.get(context);
591+
if (requestList != null) {
592+
for (RequestHandle requestHandle : requestList) {
593+
requestHandle.cancel(mayInterruptIfRunning);
594+
}
595+
requestMap.remove(context);
596+
}
586597
}
587-
requestMap.remove(context);
598+
};
599+
if (Looper.myLooper() == Looper.getMainLooper()) {
600+
new Thread(r).start();
601+
} else {
602+
r.run();
588603
}
589604
}
590605

library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,7 @@ public boolean isDone() {
166166

167167
public boolean cancel(boolean mayInterruptIfRunning) {
168168
isCancelled = true;
169-
if (mayInterruptIfRunning && request != null && !request.isAborted()) {
170-
new Thread(new Runnable() {
171-
@Override
172-
public void run() {
173-
request.abort();
174-
}
175-
});
176-
}
169+
request.abort();
177170
return isCancelled();
178171
}
179172
}

0 commit comments

Comments
 (0)