The Artima Developer Community
Sponsored Link

Design Forum
Wrapping a blocking call in a non-blocking one ...

1 reply on 1 page. Most recent reply: Apr 25, 2002 7:43 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Wrapping a blocking call in a non-blocking one ... Posted: Apr 25, 2002 6:15 AM
Reply to this message Reply
Advertisement
I was asked to present quickly a trick to convert a blocking call into a non blocking call. More precisely the method should return within a certain delay if the blocking call as not answered !

I did not sound too complicate but unless coding it with call-back, I can't seem to twist the solutionI have into smthg acceptable.

I trying smthg else but would somebody have an idea ?
One of my main concern is too have the app returning instead of simply throwing an Exception when the time is over !

I posted the original version here with some code : http://www.artima.com/forums/flat.jsp?forum=1&thread=887

Much tx,

Thomas,


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Wrapping a blocking call in a non-blocking one ... Posted: Apr 25, 2002 7:43 AM
Reply to this message Reply
Ok this post to say that I found the part of an answer:
Here is the code !
The Blocking call is this
package test.lang;
import org.apache.log4j.Category;
 
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 that this is defined, I made a threaded version of it (Wrapper class).
package test.lang;
 
public class BlockingThread
  extends Thread
{
  public Blocking blk = null;
  public Object returnedValue = null;
  
  BlockingThread ()
  {
    blk = new Blocking ();
  }
  
  public void run ()
  {
    returnedValue = blk.call ();
  }
 
  public Object getValue()
  {
    return returnedValue;
  }  
}


Finally the hardest the call:
/**
  * Who ever wants to use this code could allegdly place the following 
  * line :
  *       Based upon an idea of Duffy JohnJ
  *       
  *   as posted on yahoogroups Advanced Java mailing list 
  *   SomeType callBlockingMethod(Blocking obj, long timeout) 
  *   {
  *     Thread t = new BlockingThread(obj);
  *     t.start();
  *     t.join(timeout);
  *     if (t.isAlive()) 
  *     {
  *        return null;
  *     } else 
  *     {
  *        return t.someValue();
  *     }
  *   }
  *
  */
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();
    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));
  }
}


Tx a lot to John,
hope it helps !

Thomas,

Flat View: This topic has 1 reply on 1 page
Topic: Wrapping a blocking call in a non-blocking one ... Previous Topic   Next Topic Topic: Editor in java using JTextarea with line numbers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use