views:

394

answers:

3

Where can I find asynchronous programming example using Java? I'm interested in finding patterns in asynchronous programming for building applications that present responsiveness (preventing applications that periodically hang and stop responding to user input and server applications that do not respond to client requests in a timely fashion) and scalability.

In particularly it will be helpful to see a sample which performs I/O operations (such as file reads/writes, Web requests, and database queries) and also has a lot of CPU processing involved like a shopping suggester in a webpage.

Which are the Java libraries which can help in determining when an application's responsiveness is unpredictable - because the application's thread performs I/O requests, the application is basically giving up control of the thread's processing to the I/O device (a hard drive, a network, or whatever)

+1  A: 

In a GUI, you could use threads to perform background tasks.

Java supports non blocking I/O in the new I/O API (NIO).

If your question is more architecturally oriented, this book offers an in-depth discussion of asynchronous patterns: Patterns of Enterprise Application Architecture, by Martin Fowler.

Francois
Thanks for this...Concurrency / Martin Fowler and David Rice chapter is nice..but more towards database oriented issues discussed..
iceman
A: 

Which are the Java libraries which can help in determining when an application's responsiveness is unpredictable - because the application's thread performs I/O requests, the application is basically giving up control of the thread's processing to the I/O device (a hard drive, a network, or whatever)

If I understand you correctly, you are asking for some library that examines other threads to determine if they are blocked in I/O calls.

I don't think that this is possible in a standard JVM. Furthermore, I don't think that this would necessarily be sufficient to guarantee "responsiveness".

Stephen C
yep-both actually:know blocking calls..and then libraries which allows async I/O and handle multiple modules to run concurrently
iceman
i would like to determine when an application's responsiveness is unpredictable, from a main thread, so that i can decide how to handle it from the program
iceman
A: 

If you are using some kind of I/O operation (for example read on InputStream, which can block) you put it into a thread and the simplest solution is to use join on the thread for a given amount:

MyThread myThread = new MyThread(); 
myThread.start();
myThread.join(10000);

This join will then wait for atmost 10 seconds. After that time you can just ignore the thread, ... You can also use the Decorator pattern. You can read more here.

kovica