Skip to content
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

do not use the thread pool if it's shutdown #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions AndroidAsync/src/com/koushikdutta/async/AsyncServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;

public class AsyncServer {
Expand Down Expand Up @@ -122,17 +123,20 @@ public void removeAllCallbacks(Object scheduled) {
}

private static void wakeup(final SelectorWrapper selector) {
synchronousWorkers.execute(new Runnable() {
@Override
public void run() {
try {
selector.wakeupOnce();
}
catch (Exception e) {
Log.i(LOGTAG, "Selector Exception? L Preview?");
if (!synchronousWorkers.isShutdown()) {
synchronousWorkers.execute(new Runnable() {
@Override
public void run() {
try {
selector.wakeupOnce();
} catch (Exception e) {
Log.i(LOGTAG, "Selector Exception? L Preview?");
}
}
}
});
});
} else {
Log.i(LOGTAG, "Executor shutdown");
}
}

public Object postDelayed(Runnable runnable, long delay) {
Expand Down Expand Up @@ -399,29 +403,34 @@ public Cancellable connectSocket(final String host, final int port, final Connec
private static ExecutorService synchronousWorkers = Executors.newFixedThreadPool(4);
public Future<InetAddress[]> getAllByName(final String host) {
final SimpleFuture<InetAddress[]> ret = new SimpleFuture<InetAddress[]>();
synchronousWorkers.execute(new Runnable() {
@Override
public void run() {
try {
final InetAddress[] result = InetAddress.getAllByName(host);
if (result == null || result.length == 0)
throw new HostnameResolutionException("no addresses for host");
post(new Runnable() {
@Override
public void run() {
ret.setComplete(null, result);
}
});
} catch (final Exception e) {
post(new Runnable() {
@Override
public void run() {
ret.setComplete(e, null);
}
});
if (!synchronousWorkers.isShutdown()) {
synchronousWorkers.execute(new Runnable() {
@Override
public void run() {
try {
final InetAddress[] result = InetAddress.getAllByName(host);
if (result == null || result.length == 0)
throw new HostnameResolutionException("no addresses for host");
post(new Runnable() {
@Override
public void run() {
ret.setComplete(null, result);
}
});
} catch (final Exception e) {
post(new Runnable() {
@Override
public void run() {
ret.setComplete(e, null);
}
});
}
}
}
});
});
} else {
Log.i(LOGTAG, "Executor shutdown");
ret.setComplete(new RejectedExecutionException("Executor shutdown"), null);
}
return ret;
}

Expand Down