[concurrency-interest] ForkJoinPool for async http calls?

Gregg Wonderly gregg at cytetech.com
Thu May 10 17:42:05 EDT 2012


On 5/10/2012 12:45 PM, 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

This is completely doable in plane Java with Executor, and completely readable. 
  Annonymous, inner classes are your friend!

// This appears to be doable asynchronously, but multiple queries,
// may in fact, reduce throughput if the database is an Access
// database, or some other single threaded database, for example.
final List<String>results = new ArrayList<String>();
for( i = 0; i < q.length; ++i ) {
	final int ival = i;
	exec.execute( new Runnable() {
		public void run() {
			results.add( format( db.query( q[ival] ) ) );
		}
	});
}
exec.shutdown();

// this must be done in sequential order
for( String s : results ) {
	out.write(s);
}
out.write( "done" );

Gregg Wonderly


More information about the Concurrency-interest mailing list