[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Stopping a function performed by Thread.

Posted on 2005-04-25
14
Medium Priority
?
181 Views
Last Modified: 2010-03-31
hi all,

i've a Runnable calls in main class,
the runnable class ofcourse this is a thread, and it calls another method which downloads the stream of file of given url.
my question is that, i've a button "stop", when file stream is being downloaded and if i want to stop it, how can i do that?
0
Comment
Question by:Naeemg
  • 6
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13864359
u can try interrupting the thread but if it isn't interruptable then theres not much you can do except call stop() (which is deprecated as not safe).
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13864371
i did by calling its stop method, but it continue downloading.
how can i acheive this goal. do i need to make thread group? or any other technique?
0
 
LVL 92

Expert Comment

by:objects
ID: 13864377
can you change the code that does the downloading?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Author Comment

by:Naeemg
ID: 13864379
what kind of changes?
0
 
LVL 92

Expert Comment

by:objects
ID: 13864391
Changing the code so that it is interruptable.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13864396
how can i make my code interruptable?
0
 
LVL 92

Expert Comment

by:objects
ID: 13864429
You make sure it doesn't make any blocking calls that cannot be interrupted
and that it regularly checks if the thread has been interrupted and takes appropriate action if it has.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13864452
this is the method which download file input stream and is called by Runnable class.

//** thisURLPath = "http://www.anysite.com/anydoc.txt"
  public InputStream getInputStreamByURL(String thisURLPath)
  {
    //System.out.println(thisURLPath);
    java.net.URL thisURL = null;
    InputStream inStream = null;
    try
    {
      thisURL = new java.net.URL(thisURLPath);
      try
      {
        URLConnection connection = thisURL.openConnection();
        inStream = connection.getInputStream();
      }
      catch (UnknownHostException e)
      {
        //System.out.println("Unknown Host");
      }
      catch (IOException e)
      {
        //System.out.println("Error in opening URLConnection, Reading or Writing");
      }
    }
    catch (MalformedURLException ex)
    {
      //ex.printStackTrace();
    }
    catch (IOException ex)
    {
      //ex.printStackTrace();
    }

   return inStream;
  }
0
 
LVL 92

Expert Comment

by:objects
ID: 13864486
You might need to add a connect timeout.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13864551
how do that?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 13864896
System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13869073
If you want the getInputStreamByURL method to timeout while you are establishing the connection then you can use the suggestion above or use
the Java 1.5 URLConnection#setConnectTimeout.
If you want to stop the thread that is using the inputstream (given by the getInputStreamByURL method) you can close the stream (inputstream.close()) which will cause the read operation
to get IOException and terminate.
You might also want to consider setting the SoTimeout so your blocking read will not block forever.
You can do that by either using the system property: System.setProperty("sun.net.client.defaultReadTimeout", "5000");
or when using java 1.5 call URLConnection#setReadTimeout
0
 
LVL 5

Author Comment

by:Naeemg
ID: 14587377
sorry , i forgot to accept the answer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question