Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Async IO threading model

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I just figured there's the new Async stuff.
If I call a write operation and get back a future. It means that the calling thread is free to proceed. That leaves the question in what thread does the writing happen?
Similarly, where do completion handlers run?
Where can I read more about this?

Thanks.
 
Mike Simmons
Ranch Hand
Posts: 3039
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:Hi,

I just figured there's the new Async stuff.
If I call a write operation and get back a future. It means that the calling thread is free to proceed. That leaves the question in what thread does the writing happen?


That depends on how the "write operation" you call is implemented. Can you be more specific? What concurrency classes and methods are you using?

A common scenario involving a Future would be if you're using an ExecutorService and called submit(). The question would be, what sort of ExecutorService is this, and how was it configured? Often it might have been created by a call to Executors.newFixedThreadPool(). In that case, tasks will run on one of the threads in the thread pool. Other implementations of ExecutorService might do other things.

marwen Bakkar wrote:Similarly, where do completion handlers run?

Similarly, depends on the the implementation of the completion handler. Often there's a thread pool involved.

quote=marwen BakkarWhere can I read more about this?
The main options are:

1. The Java API
2. Various free online articles about the java.util.concurrent packages released in JDK 5 and later. (Try googling "java ExecutorService", for example)
3. A book such as Java Concurrency in Practice

I recommend the last of these in particular, but all the info you need is present in free online sources, if you read them carefully enough.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well since this is Java 7 forum I thought it'd be clear I'm referring to NIO2 channels, such as AsynchronousSocketChannel. Sorry for the confusion.

I found this in the package description :

Asynchronous channels are bound to an asynchronous channel group for the purpose of resource sharing. A group has an associated ExecutorService to which tasks are submitted to handle I/O events and dispatch to completion handlers that consume the result of asynchronous operations performed on channels in the group. The group can optionally be specified when creating the channel or the channel can be bound to a default group. Sophisticated users may wish to create their own asynchronous channel groups or configure the ExecutorService that will be used for the default group.
 
Mike Simmons
Ranch Hand
Posts: 3039
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, interesting.

It looks like you start with something like

And which threads run the tasks is determined by the implementation of ExecutorService that you pass in, which is typically some sort of thread pool.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic