Make a Blocking call non - blocking
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Thomas_SMETS
Posted On:   Wednesday, April 24, 2002 05:46 PM

I would like to make a simple call currently blocking, non-blocking. Say the class Blocking has one method call (w/o parameters).

I want ot create a API call (int delay) that would simply invoke the call () & return null if the specified delay is over.

The stupid solution I worked out up untill now are not OK ...

Would some one have a pointer for me ?
Bright idea are always welcome too !




Thomas SMETS ,
SCJP, Brussels

Re: Make a Blocking call non - blocking

Posted By:   Thomas_SMETS  
Posted On:   Thursday, April 25, 2002 10:04 AM

This is just the code I managed to make & that solved the problem !

Here are the classes :


package test.lang;
import org.apache.log4j.Category;
/**
* This class i ssimply making a blocking call for a defined period (provided as paramter)
* @author Thomas SMETS
*/
public class Blocking {
public static final String MESSAGE
= "This is the message to access in the Blocking call";
public static final int MAX_TIME = 2500;

Category log;

public Blocking() {
log = Category.getInstance (Blocking.class);
}

public String call (int aDelay) {
log.info ("Blocking call");
try {
Thread.sleep (aDelay);
}catch (Exception e) {
log.warn ("Thread was Interrupted");
}
return MESSAGE;
}

public String call () { return this.call (MAX_TIME); }
}



Now, the second step is to Wrap the (blocking) class in a Threaded one. Very simple but make the calls clearer (to me at least)

package test.lang;

public class BlockingThread
extends Thread
{
public Blocking blk = null;
public Object returnedValue = null;

BlockingThread () { blk = new Blocking (); }
/** Note that the run only contains the call to the blocking call*/
public void run () { returnedValue = blk.call (); }

public Object getValue() { return returnedValue; }
}





/**
* Who ever wants to use this code could allegdly place the following
* line :
* Based upon an idea of Duffy JohnJ
* See Advanced-java ML archive for original post :
* https://lists.xcf.berkeley.edu/lists/advanced-java/
* or http://groups.yahoo.com/group/advanced-java/
*/
package test.lang;

import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;

public class NonBlockingWrapper {
public Object callBlockingMethod(long timeout) {
BlockingThread t = new BlockingThread();
t.start();
// This line is really helpfull making sure that the timeout
// period is certianly not superior to the one that was given as parameter to the timeOut.
Thread.currentThread ().yield ();
try { t.join(timeout); }
catch (Exception ex) { System.out.print ("Exception thrown"); }

if (t.isAlive()) {
return null;
} else {
return t.getValue ();
}
}

/**
*
*/
public static void main (String[] args)
{
BasicConfigurator.resetConfiguration ();
BasicConfigurator.configure ();
NonBlockingWrapper NBW = new NonBlockingWrapper();
System.out.println ("Response is " + NBW.callBlockingMethod (3500));
}
}



For any question,

please feel free ...







Thomas SMETS,

SCJP, Brussels

Re: Make a Blocking call non - blocking

Posted By:   Simon_Ablett  
Posted On:   Thursday, April 25, 2002 08:57 AM

I guess your approach would need to take into account why the call was blocking. Generally speaking, interrupting a blocked IO call is not a good idea. After all, they generally block for a good reason. For example, interrupting a blocking IO operation on a database would not be a good idea as it could leave the database in an indeterminate state. This is why using 'interrupt' to interrupt a thread under Solaris is not a good idea because under Solaris, unlike with other OSes, the JVM causes a blocked IO op to generate an InterruptedIOException.

Regards.
About | Sitemap | Contact