• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically terminate code which my program can not access?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making a call to an api, which handles the uploading of a file. I would like to be able to cancel the upload upon user request. But, the api is poorly implemented, it does not expose an sort of a cancel method. Once the upload is started, there is no way to force it stop. Any suggestions about what I could do?

What makes some sense to me is to do something along the lines of making the upload call in a thread, keeping a reference to the thread, and killing upon user request. But, according to the java api, this is a bad idea:

"
stop()
Deprecated. This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait.
"

Modifying a global variable doesn't get me anywhere. Because the upload is happening inside the api, which is code that I can not modify. Help please?

Thank you
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree that killing and stopping threads is dangerous.

There are all sorts of things you need to consider, and ought to tell us before deciding, eg
  • Where are you uploading from?
  • Is your method suitable for such an upload?
  • Is your method principally designed for uploads of different sizes or from different locations?
  • Do you have to open and close Sockets or similar?
  • Can you find a different uploading method?
  • Can you start a Thread which handles the upload, and throw an Exception in that Thread to cancel it?

  • The whole thing might be very complicated.
     
    Johann Dobbins
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Agree that killing and stopping threads is dangerous.

    There are all sorts of things you need to consider, and ought to tell us before deciding, eg

  • Where are you uploading from?
  • Is your method suitable for such an upload?
  • Is your method principally designed for uploads of different sizes or from different locations?
  • Do you have to open and close Sockets or similar?
  • Can you find a different uploading method?
  • Can you start a Thread which handles the upload, and throw an Exception in that Thread to cancel it?

  • The whole thing might be very complicated.



    Where are you uploading from? Not sure what you mean, users will be uploading from my app on their machine.
    Is your method suitable for such an upload? The method provided by the api? Yes, it is suitable, other than the lack of a cancel.
    Is your method principally designed for uploads of different sizes or from different locations? It can handle uploads of any size, not sure what is meant by "different locations"
    Do you have to open and close Sockets or similar? I'm not positive what the api is doing, I assume it is making webservices calls, so I'm going to say no.
    Can you find a different uploading method? No, for maintenance purposes, I am required to use the provided api.
    Can you start a Thread which handles the upload, and throw an Excpetion in that Thread to cancel it? I don't think so. Since I have no access to the api code, I can not write any logic to receive and handle an Exception.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Don't know, I am afraid.
     
    Johann Dobbins
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Don't know, I am afraid.



    Lol. Thanks. I didn't particularly expect a solution.
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Instead of calling stop() on the thread doing the upload, you could try calling interrupt() instead.
     
    We're being followed by intergalactic spies! Quick! Take this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic