[concurrency-interest] ForkJoinPool for async http calls?

Roland Kuhn rk at rkuhn.info
Thu May 10 14:37:35 EDT 2012


On May 10, 2012, at 19:45 , Zhong Yu wrote:

> On Thu, May 10, 2012 at 12:13 PM, √iktor Ҡlang <viktor.klang at gmail.com> wrote:
>> 
>> 
>> On Thu, May 10, 2012 at 7:07 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>>> 
>>> On Thu, May 10, 2012 at 10:26 AM, Christian Essl
>>> <christianessl at googlemail.com> wrote:
>>>> Your are right my primary concern is code-simplicity not pure
>>>> performance, basicly writing in a traditional imperative blocking
>>>> style but still having IO with (mostly) non-blocking threads (and no
>>>> callbacks, no monadic futures).
>>> 
>>> Thread is such a nice programming abstraction, it's a shame that we
>>> are so concerned of its overhead nowadays. Replacing simple threads
>>> with complex tasks seems to be retrogressing - aren't programming
>>> supposed to become easier?
>>> 
>>> What are the fundamental reasons that Java Threads are expensive?
>> 
>> 
>> Can you give some arguments as to the greatness of proactive programming
>> (threads)?
> 
> It just seems straightforward to me to write and read
> 
>    for(i=0;i<10;i++)
>    {
>        result = db.query( q[i] );  // blocking
>        s = format(result);
>        out.write( s );  // blocking
>    }
>    out.write("done") // blocking
> 
!!! caution: Scala code ahead !!!

Future.flow {
  var i = 0
  while (i < 10) {
    val resultFuture = dbActor ? Query(q(i))
    outputActor ! format(resultFuture())
    i += 1
  }
  outputActor ! "done"
}

I’ve just seen Viktor’s reply, but I think that mine (while not being idiomatic Scala) might be clearer in this context ;-)

Explanation: the flow-block will be transformed by the compiler into continuation-passing style around resultFuture(), effectively breaking it up so that at this point execution is suspended until the result of the future is ready. Imperative coding style, asynchronous execution, profit.

Regards,

Roland

--
Simplicity and elegance are unpopular because they require hard work and discipline to achieve and education to be appreciated.
  -- Dijkstra




More information about the Concurrency-interest mailing list