• Post Reply Bookmark Topic Watch Topic
  • New Topic

Urgent threading issue

 
Pappu Kumar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a helper class , whose method i invoke . The problem is that method can take upto 15 sec to finish. But i only want to wait 10 sec...if it doesnt return in 10 sec...i would like to kill that method call.


I am thinking i have to spawn new threads...one for the method call...another to keep track of time.....
Can someone tell me how to accomplish this.
Pappu
 
V Chauhan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can create a separate thread for keeping track of all helper threads. It can store information about when each thread was started. Update that information once the thread completes.

You can periodicaly check which are threads those were started 10 mins back and kill them..

Well for killing them you have to either invoke stop (which is deprecated and not recommended at all) or you can interrupt the threads.

The more elegant solution will be to set a flag on the helper object. And you can check in your method at certain points and decide whether to proceed or not.

Hope it helps.
 
Pappu Kumar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it something like this :

***************************
import java.util.*;

class test1 extends Thread
{
public void run(){
for (int i=0;i<=6 ;i++ )
{
System.out.println("Sleeping "+ i + " second.");
try {
long numMillisecondsToSleep = 1000; // 1 second
Thread.sleep(numMillisecondsToSleep);
} catch (InterruptedException e) {
System.out.println("INTERUPTED");
return;
}
}
}
}

public class Test
{
public Test(){
try
{
Thread thread = new test1();
thread.start();
Thread.sleep(10000);
if(thread.isAlive())thread.interrupt() ;
}
catch (Exception e)
{
e.printStackTrace();
}

}

public static void main(String[] args)
{
new Test();
}
}
*****************************


But in this case i always sleep for 10 sec.....as you can see my sample test1 thread finishes in 6 sec......how do i avoid sleeping for that extra 4 sec in case my helper thread finishes before 10 sec. ???

Please suggest
 
V Chauhan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please make your code more readable by using the CODE tags while posting here.

If you want to work along the lines, you can interrupt the thread which is sleeping for 10secs. Suppose you have two threads T1 and T2. T1 is your worker thread. T2 sleeps for 10 secs and then interrupts T1 if it is still alive.

Similarly T1 can interrupt T2 as soon as it is over with the task. T2 can catch the InterruptedException and proceed.

Hope this helps.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pappu Kumar:
I have a helper class , whose method i invoke . The problem is that method can take upto 15 sec to finish. But i only want to wait 10 sec...if it doesnt return in 10 sec...i would like to kill that method call.

If you're doing this as an exercise to learn the underpinnings of threading in Java, you'll want to skip this reply. If instead you're just trying to solve a problem, I recommend using Doug Lea's Concurrent library. I've used it in the past with success for thread pooling and job queues, and it's being rolled into J2SE 1.5 as java.util.concurrent.

Here is the main class you'll find useful: TimedCallable. And the brief description:
TimedCallable runs a Callable function for a given length of time. The function is run in its own thread. If the function completes in time, its result is returned; otherwise the thread is interrupted and an InterruptedException is thrown.

[ August 19, 2004: Message edited by: David Harkness ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!