The Artima Developer Community
Sponsored Link

Java Answers Forum
Convert Blocking call in non-blocking call

5 replies on 1 page. Most recent reply: Apr 25, 2002 3:24 PM 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 5 replies on 1 page
Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Convert Blocking call in non-blocking call Posted: Apr 24, 2002 4:14 PM
Reply to this message Reply
Advertisement
I was asked to present quickly a trick to convert a blocking call into a non blocking call.

I did not sound too complicate but the program here under does not work !

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

Much tx,

Thomas,


package test.lang;
 
// get the latest Log4J class from jakarta.apache.org/Log4J/
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 NoBlockingCall  
{
  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 (NoBlockingCall.class);
  
  String AStringToPopulate = null;
  
  public String call (int aTimeToWait )
  {
    try
    {
      new Timer (aTimeToWait, new ToRun()).start ();
      // Place here your blocking call that should be non-blocking
      AStringToPopulate = new Blocking().call();
    }catch (RuntimeException CEe)  
    {
      log.info ("Exception thrown " + CEe.getClass().getName());
      AStringToPopulate = null;
    } finally
    {
      //log.info ("In finally ");
    }
    return AStringToPopulate;
  }
 
/** 
  *
  */
  public static void main (String[] args)
  {
    BasicConfigurator.resetConfiguration ();
    BasicConfigurator.configure ();
    Category sLog = Category.getInstance (NoBlockingCall.class);
    sLog.info ("Started");
    NoBlockingCall nbc = new NoBlockingCall ();
    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 RuntimeException ("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;
    }
  } 
}  // made with www.jcreator.com


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Convert Blocking call in non-blocking call Posted: Apr 24, 2002 11:48 PM
Reply to this message Reply
I haven't looked to closely, yet, but shouldn't you start your timer in another thread?

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Convert Blocking call in non-blocking call Posted: Apr 25, 2002 12:51 AM
Reply to this message Reply
?
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);
    }
  }   
}

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Convert Blocking call in non-blocking call Posted: Apr 25, 2002 8:17 AM
Reply to this message Reply
There is the answer :
http://www.artima.com/forums/flat.jsp?forum=17&thread=891
Thomas,

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: Convert Blocking call in non-blocking call Posted: Apr 25, 2002 1:56 PM
Reply to this message Reply
hi Thomas..

is this program do a calling from computer to telephone?

if yes can you tell me how to do something like this ?

or do you have links?

thanks

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Convert Blocking call in non-blocking call Posted: Apr 25, 2002 3:24 PM
Reply to this message Reply
Hi Maysoon,

I dunno what you mean exactly ...
R U taking a piss at me or ... ?

>
> is this program do a calling from computer to
> telephone?
>
> if yes can you tell me how to do something like this
> ?
>
> or do you have links?
>


Nope I haven't got links & I do not believe the program is for telephone in particular. You need to use the jTAPI's for that (personnaly never used)!

This is google's response :
http://www.google.com/search?sourceid=navclient&querytime=wcF93B&q=jtapi

All the links seem to be of high relevance !

The programs show how to convert a Blocking call (class Blocking), into a smart non-blocking call.
Smart means :
_ failure to meet the dead line is not returned by an Exception
_ returned value is not via a call-back (see the usual event model, as this is way simpler in my confused mind :-) ).
_ Though using Threading the conversion from the blocking call to the non-blocking call is really straight forward (almost monkey typing).


So can you explain me why you are refering to telephone ?

Rgds,

Thomas,

Flat View: This topic has 5 replies on 1 page
Topic: How to serach a text file and display the results Previous Topic   Next Topic Topic: Function sequence error??????

Sponsored Links



Google
  Web Artima.com   

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