0

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?

Chao Jiang
  • 135
  • 1
  • 10
  • Where is the code for the UploadableTask? Please add it. – A_C Dec 09 '18 at 05:38
  • There is no straight answer, as interruptions requires collaboration from the targeted thread. In your case, it depends on much control and/or low-level access you have on the `UploadableTask` (for instance, if you can access the underlying socket, etc.). See [related topics](https://stackoverflow.com/questions/4556401/how-to-stop-uninterruptible-threads-in-java). – Alexandre Dupriez Dec 09 '18 at 09:47

1 Answers1

0

... probably will not work ...

is a rather unproductive way of reasoning. Why not just test it? It might probably work!!

But yes, i also believe that probably it wont work. In that case question is do you really need to cancel it? It is an upload process anyway. What harm can it doâ„¢? Also remember, even if everything inside java works just fine, you still have no deterministic way to cancel the upload itself. Due to the very nature of http and networks in general, (assuming it is an http upload) there is no deterministic way to cancel once the data leaves the jvm - which it does pretty fast. Jvm is just waiting for acknowledgement of receipt, not actual transmission. So, do you really need to cancel it?

If you do, then the only answer is that threads are an unsuitable choice for cancellable and rather long running activities. If you are ready to pay the price, you can choose to fork out a separate process - which can be deterministically killed. Go for ProcessBuilder and carve out the upliad code in a different jar. An http upload is still not guranteed to die.

inquisitive
  • 3,549
  • 2
  • 21
  • 47
  • Thanks. I just test it. future.cancel(true); can make the main thread return. But the upload thread keep uploading data. The main purpose is to protect thread resources when the upload SDK has network problems. So clearly cancel method can save main thread. But it wont stop the uploading thread. When we have lots of requests, that will be a disaster. – Chao Jiang Dec 09 '18 at 07:34