Stop flow when user cancel command

The mobile application provides the user with the ability to download email attachments from a remote site. Connecting to a remote server and downloading content is carried out in a separate stream. The dialog is displayed to the user using the cancel.Herewith command I am providing pseudocode.

new Thread(new Runnable() public void run(){ try{ //open connection to remote server //get data input stream //create byte array of length attachment size //show modeless dialog with the message "Downloading..." for(int i=0;i<attachmentSize;i++){ //set the progress indicator of the modeless dialog based upon for iteration //read the byte from input stream and store it in byte array } //open file connection outputstream and store the downloaded content as a file in mobile file system //show dialog with the message "attachment successfully downloaded" } catch(IOException ioe) { } catch(Exception ex) { } } ).start(); 

Now I am in the process of adding the cancel command to the dialog with the progress indicator. When the user clicks the “Cancel” button on the mobile phone, the modeless dialog can be deleted by calling the dispose () method. How can I abruptly stop the stream that receives email attachments through streaming? Please help me solve this problem.

+4
source share
4 answers

You can stop it all of a sudden, but it brings more problems than it costs.

The canonical way to do this is to have a flag that is checked in Runnable:

 public class ClassHoldingRunnable { private volatile boolean stopRequested = false; public void executeAsync() { Runnable r= new Runnable() { public void run() { while ( !stopRequested ) { // do work } } } new Thread(r).start(); } public void cancel() { stopRequested = true; } } 

A few notes:

  • It is vital that the stopRequested flag is either volatile or has a different guarantee of visibility ( synchronized , Lock , Atomic ), since several threads access it;
  • You should often check stopRequested if it is important for the end user to have a flexible graphical interface;
+2
source

There are several free ways to interrupt a stream that is read from Connection.

  • You are probably reading the remote data by going through a single call to InputStream.read so that you can reuse one relatively small byte [] object. You can check the boolean member variable before each iteration. You do not need to synchronize around this boolean, because it only needs to change the value once after building the Thread.

  • Closing the connection means that your thread will throw an IOException when it tries to access it. A proper JavaME implementation will not make the Connection.close () block, even if another thread is reading from the InputStream of the connection.

+1
source

I am not an expert on this subject, so make my suggestion with salt, as my experience is very limited in Java threads.

You cannot stop the current thread. You can just get out of it as soon as possible. So, what you can do is to have, for example, a common flag that you periodically check in the secondary stream. When the main thread sets it in response to clicking Cancel, the secondary thread returns.

0
source

My experience is more about C #, but it is still possible ...

I don’t think it’s a good idea to find a way to just “kill the thread”, more than just deleting the object and skipping its destructor.

You can say that the stream committed suicide through interruption. Then you can use the thread interrupt flag as an indicator or, if you have a sleep / wait there, you can catch the interrupted exception and close it correctly if it is caught (in the finally block). This should provide more or less what you are looking for.

0
source

Source: https://habr.com/ru/post/1286506/


All Articles