DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Comparing Java 7 Async NIO with NIO.

Comparing Java 7 Async NIO with NIO.

Peter Lawrey user avatar by
Peter Lawrey
·
Aug. 06, 11 · Interview
Like (0)
Save
Tweet
Share
13.16K Views

Join the DZone community and get the full member experience.

Join For Free

SDP (Sockets Direct Protocol) in Java 7 promises better integration with InfiniBand. This comes with a new Socket classes AsynchronousSocketChannel and AsynchronousServerSocketChannel. Is there any advantage in using these libraries if you don't have InfiniBand?

Creating a server socket

The API for AsynchronousServerSocketChannel is slightly different to ServerSocketChannel. The most notable difference is the use of Futures instead of always blocking.
final AsynchronousServerSocketChannel ssc =
      AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));
Future<AsynchronousSocketChannel> accepted = ssc.accept();
AsynchronousSocketChannel sc2 = accepted.get();

ByteBuffer bb = ByteBuffer.allocateDirect(4096);
Future<Integer> readFuture = sc2.read(bb);

Creating a client socket

Similarly the client can start a connection and later wait when the connection is really needed.
AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
Future connected = sc.connect(new InetSocketAddress("localhost", 9999));
// later ensure we are connected.
connected.get();

ByteBuffer bb = ByteBuffer.allocateDirect(4096);
// populate bb
Future<Integer> writeFuture = sc2.write(bb);

Is there any performance differences?

Without threads, the Asynchronous IO was 5-10% faster. However once threads were used, the performance dropped. This could be because this is Java 7 update 0. I look forward to testing this again in the future.

Async Socket latency was 1/50/99%tile 5.2/5.4/7.3 us
Async Socket Throughput was 186 K/s
Threaded Async Socket Latency for 1/50/99%tile 13.8/16.9/24.7 us
Threaded Async Socket Throughput was 183 K/s
The best of three runs was used.

For comparison, the same tests using regular NIO on Java 7 were
Socket latency was 1/50/99%tile 5.6/5.8/7.0 us
Socket Throughput was 170 K/s
Threaded Socket Latency for 1/50/99%tile 6.0/8.5/10.7 us
Threaded Socket Throughput was 234 K/s

The code

Its worth nothing that the code is slightly more complex to use than regular NIO. However, the features added are powerful and for the brave, it will be valuable.

Asynchronous NIO Socket - AsyncPingTest.java

Plain NIO Socket - PingTest.java

Conclusion

The API for Asynchronous sockets looks promising despite the added complexity. However the performance many not be there unless you have the right hardware, yet.

 

From http://vanillajava.blogspot.com/2011/08/comparing-java-7-async-nio-with-nio.html

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Solving the Kubernetes Security Puzzle
  • Full Lifecycle API Management Is Dead
  • Multi-Cloud Integration
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: