aspose file tools*
The moose likes Java in General and the fly likes A Timeout scenario Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "A Timeout scenario" Watch "A Timeout scenario" New topic
Author

A Timeout scenario

pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 26
Hi Everyone,

I am trying to solve a time-out scenario using Timer,TimerTask classes with a little bit of threading concept.

Please find below what i am trying to solve:

1. I have a method in class classA : doTheGivenTask().This method should do the following 4 tasks Please refer to the code which i have written)

a) Invoke the constructor of ClassC.
b) Start timer.
c) Invoke the following methods in classC:
sendRequest()
getResponse()
and...
processRequest() Of class classB.
All the above mentioned methods must be completed in say 10 secs.
and the control should be back in class classA.
If the above mentioned 3 tasks are not completed within 10sec,then
the task should be cancelled and control should be back in class classA.
If the above mentioned tasks are completed within 10 sec,say 5 sec then
immediately the control should be back in class classA.It should not be
waiting till 10 sec to get back to class classA.
d) Stop the timer.

I have tried to do this task but got stuck.I am able to invoke all the three methods in 10 sec,but i am not able to code the logic when all the three methods are completed under 10 sec.In this case , the control should return to class classA back immediately,instead of waiting for 10 sec.
So ,please help me in moving forward.

Please find the my source code below:



import java.util.*;

public class TimerExample
{
/**
* @param args
*/
public static void main(String[] args) throws InterruptedException
{
new classA().doTheGivenTask();
}
}

//TimerTask class which will invoke the Class classC Methods.
class taskToDo extends TimerTask
{
classC c_obj = null;
boolean requestFlag = false;
boolean responseFlag = false ;

public taskToDo(classC c_obj)
{
this.c_obj = c_obj;
}

public void run()
{
try
{
invokemethod();

}
catch(InterruptedException e)
{
}
}

void invokemethod() throws InterruptedException
{
requestFlag = c_obj.sendRequest();
if (requestFlag)
{
System.out.println("Given task is completed within the given time frame");
responseFlag = c_obj.getResponse();
}

//nomatter whether requestFlag is true of false invoke processRequest() of classB
classB b_obj = new classB();
b_obj.processRequest();
}
}


class classA
{
Timer timer = null;

taskToDo givenTask = null;

boolean responseFlag = false;

public void doTheGivenTask() throws InterruptedException
{
classC c_obj = new classC();

timer = new Timer();

givenTask = new taskToDo(c_obj);

timer.schedule(givenTask, 0);

//pause for 10 seconds for the method call.
Thread.sleep(10000);

timer.cancel();


System.out.println("requestFlag : " + givenTask.requestFlag);
System.out.println("responseFlag : " +givenTask.responseFlag);

}
}


class classC
{

boolean sendRequest() throws InterruptedException
{


Thread.sleep(2000);
return true;
}

boolean getResponse()
{
Thread.sleep(3000);
return true;
}
}


class classB
{
public void processRequest()
{
System.out.println("ProcessRequest of classB is invoked");
}

}

Advance Thanks.
raminjavac.
Manuel Palacio
Ranch Hand

Joined: Oct 16, 2000
Posts: 45
You should check the classes in the java.util.concurrent package like:

FutureTask

These classes should meet most of your "multithreaded" needs.

For example you could get rid of the timer and just use Future like this:



get() will return when the task is done, so you don't have to wait for 10 s even if it's done. You could also ask the task if it's done after 5s, if not cancel it, etc.
[ August 30, 2006: Message edited by: Manuel Palacio ]

<a href="http://www.newinstance.net" target="_blank" rel="nofollow">http://www.newinstance.net</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In 1.3 I didn't have FutureTask and did this, which works out rather the same way ...

There's a bit more to it, of course. Handle the interrupted exception from wait, maybe try to interrupt ttt so rrr can tell it doesn't have to keep working. rrr has to notify() ... it might be neater to do ttt.join() instead of rrr.wait() so rrr only has to complete the run() method.

(Changed one-r to rrr because I talk good English.)


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 26
Hi All,
Thanks Manuel Palacio & Stan James.
Placio,your reply was based on java1.5.It was informative.Thanks for that.I want to code in java 1.4.

James,You came closer to answering my concern! But what i am really looking for is,how to break the invokemethod() call if it is done in less than 87 seconds.I am getting stuck at that point.

So,please help me to proceed forward.

Thanks!
Pradeep.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The snippet I gave you will either return good data in less than n seconds, or throw an exception after n seconds. I think that structure solves your problem, though the exception bit might not be what you want. Try building it into your own code and see what does or doesn't work for you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A Timeout scenario
 
Similar Threads
Parsing a variable
Java Polyphormismus BUG (solved: not a bug:-)
Why runtime exception thrown is this code
Question on Constructor, instance var & static var
Question regarding encapsulation