Thomas SMETS
Posts: 307
Nickname: tsmets
Registered: Apr, 2002
|
|
Re: Convert Blocking call in non-blocking call
|
Posted: Apr 25, 2002 12:51 AM
|
|
? Yeap somehow that is what I should do probably but I thought that it was making a russian doll (you know, the thread inthe thread, in the ...).
If what you meant was having this (full code at the bottom):
public String call (int aTimeToWait )
{
final Timer t = new Timer (aTimeToWait, new ToRun());
Blocking blk = new Blocking ();
try
{
new Thread(
new Runnable ()
{
public void run ()
{
t.start ();
}
}
).start();
// Place here your blocking call that should be non-blocking
AStringToPopulate = blk.call();
}catch (RuntimeException REe)
{
log.info ("Exception thrown " + REe.getClass().getName());
t.stop ();
//t = null;
AStringToPopulate = null;
} finally
{
//log.info ("In finally ");
}
return AStringToPopulate;
}
Nope, it doesn't work, yet :)
Tx,
Thomas,
Complete program :
package test.lang;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
public class NonBlockingCall1
{
public static final int MAX_TIME_TO_WAIT = 1000;
public static final int TIME_TO_SLEEP = 5000;
public static final String MESSAGE
= "This is the message to access in the Blocking call";
Category log = Category.getInstance (NonBlockingCall1.class);
String AStringToPopulate = null;
public String call (int aTimeToWait )
{
final Timer t = new Timer (aTimeToWait, new ToRun());
Blocking blk = new Blocking ();
try
{
new Thread(
new Runnable ()
{
public void run ()
{
t.start ();
}
}
).start();
// Place here your blocking call that should be non-blocking
AStringToPopulate = blk.call();
}catch (RuntimeException REe)
{
log.info ("Exception thrown " + REe.getClass().getName());
t.stop ();
//t = null;
AStringToPopulate = null;
} finally
{
//log.info ("In finally ");
}
return AStringToPopulate;
}
/**
*
*/
public static void main (String[] args)
{
BasicConfigurator.resetConfiguration ();
BasicConfigurator.configure ();
Category sLog = Category.getInstance (NonBlockingCall1.class);
sLog.info ("Started");
NonBlockingCall1 nbc = new NonBlockingCall1 ();
sLog.info ("Returned value : " + nbc.call (MAX_TIME_TO_WAIT));
}
public class ToRun
implements ActionListener
{
ToRun ()
{
log.info ("ToRun created");
}
public void actionPerformed (ActionEvent anActionEvent)
throws RuntimeException
{
log.info ("Start the Run-up time");
throw (new TimerDelayException ("This is my Exception from the ToRun"));
}
}
public class Blocking
{
public String call ()
{
log.info ("Blocking call");
try
{
Thread.sleep (TIME_TO_SLEEP);
}catch (Exception e)
{
log.warn ("Thread was Interrupted");
}
return MESSAGE;
}
}
public class TimerDelayException
extends RuntimeException
{
TimerDelayException (String aMsg)
{
super (aMsg);
}
}
}
|
|