iceman
iceman

Reputation: 4261

Asynchronous programming using Java

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)

Upvotes: 1

Views: 9066

Answers (5)

Miguel Gamboa
Miguel Gamboa

Reputation: 9393

For examples performing asynchronous operations with emphasis on non-blocking IO on files, you may check some samples here: https://github.com/javasync/idioms (disclaimer I am the author).

I use this samples in introduction to asynchronous programming in Java and we explore callback based, CompletableFuture and finally reactive streams.

Upvotes: 2

Samy Omar
Samy Omar

Reputation: 810

in a web environment, you can make use of the new j2ee6 Asynchronous feature. take a look at http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html

Upvotes: 0

kovica
kovica

Reputation: 2583

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.

Upvotes: 0

Stephen C
Stephen C

Reputation: 719606

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".

Upvotes: 0

Francois
Francois

Reputation: 2369

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.

Upvotes: 2

Related Questions