Currently, I use a SDK to upload images. However, the upload method have not provided timeout option. Therefore I need to set timeout manually. One option is :
public static String uploadImg(String imgStream) {
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future<String> future = executor.submit(new uploadableTask(imgStream));
try {
final String res = future.get(500, TimeUnit.SECONDS);
return res;
} catch (final TimeoutException e) {
future.cancel(true);
executor.shutdownNow();
return null;
} catch (final InterruptedException e) {
return null;
} catch (final ExecutionException e) {
return null;
}
}
However, future.cancel(true); probably will not work. It depends on the implementation of upload method of SDK in uploadableTask class(ex. If the interrupt flag will not be checked, the upload method cannot stop). Anyone know other solutions to do that?