Java NIO - PowerPoint PPT Presentation

About This Presentation
Title:

Java NIO

Description:

A short 'tutorial-like' introduction to NIO. On NIO and patterns for distributed systems ... Started off as a JSR under Sun's JCP (JSR51) ... – PowerPoint PPT presentation

Number of Views:504
Avg rating:3.0/5.0
Slides: 38
Provided by: andre53
Category:
Tags: nio | java | jcp

less

Transcript and Presenter's Notes

Title: Java NIO


1
Java NIO
  • Andrew Borg

2
This Presentation
  • What NIO provides
  • A short tutorial-like introduction to NIO
  • On NIO and patterns for distributed systems
  • Why NIO is important to us
  • Direct Memory Access and the RTSJ
  • Nonblocking IO in RMI

3
This Presentation
  • What NIO provides
  • A short tutorial-like introduction to NIO
  • On NIO and patterns for distributed systems
  • Why NIO is important to us

4
What is Java NIO?
  • Stands for New IO.
  • Started off as a JSR under Suns JCP (JSR51).
  • APIs for scalable I/O, fast buffered binary and
    character I/O, regular expressions, charset
    conversion, and an improved filesystem
    interface.

5
How does it do it?
  • An API for scalable I/O operations on both files
    and sockets, in the form of either asynchronous
    requests or polling
  • An API for fast buffered binary I/O, including
    the ability to map files into memory when that is
    supported by the underlying platform
  • An API for fast buffered character I/O, including
    a simple parsing facility based upon regular
    expressions and a simple printf-style formatting
    facility

6
and
  • An API for character-set converters, including a
    service-provider interface for pluggable
    converters
  • A rich yet platform-independent set of I/O
    exceptions
  • A new filesystem interface that supports bulk
    access to file attributes (including MIME content
    types), escape to filesystem-specific APIs, and a
    service-provider interface for pluggable
    filesystem implementations

7
JDK 1.4.x
  • Provides the package java.nio
  • This package is a subset of JSR-51. In
    particular, proposed file-handling functionality
    is absent

8
Why so long for good IO?
  • Because it is hard for a platform independent
    development platform to provide functionality
    common to all platforms
  • Not all OS provide the same functions. IO
    depends very much on the underlying OS. For
    example file metadata is OS specific
  • Many of the IO features require native code

9
So what does NIO give us?
  • A lot but not everything
  • Native code is still required but you lose
    portability
  • but some of the things that could be done before
    only with native code are now provided in NIO

10
This Presentation
  • What NIO provides
  • A short tutorial-like introduction to NIO
  • On NIO and patterns for distributed systems
  • Why NIO is important to us

11
The java.nio API
  • java.nio
  • java.nio.
  • java.nio.charset and java.nio.charset.spi
  • java.nio.channels and java.nio.channels.spi
  • (java.util.regex)
  • Minor Changes in other packages

12
java.nio.
  • A whole set of abstract buffers classes, all
    extending the abstract class java.nio.buffer
  • eg ByteBuffer, CharBuffer, DoubleBuffer,
    FloatBuffer, IntBuffer, LongBuffer, ShortBuffer
  • Set of methods reset(), flip(), etc.
  • 0 lt mark lt position lt limit lt capacity

13
So what is special about these buffers?
  • Direct allocation to physical memory is allowed
    using native methods if possible
  • May be mapped to a file (memory-mapped files).
  • An implementation may use a backing array.
  • Different views for byte buffers
  • JNI methods

14
JNI functions for NIO direct buffers
  • jobject NewDirectByteBuffer(JNIEnv env, void
    address, jlong capacity)
  • void GetDirectBufferAddress(JNIEnv env, jobject
    buf)
  • jlong GetDirectBufferCapacity(JNIEnv env,
    jobject buf)

15
Memory and the RTSJ
  • The RTSJ had to think about this too.
  • RawMemoryAccess
  • VT and LT PhysicalMemory are mapped to physical
    addresses
  • public LTPhysicalMemory(java.lang.Object type,
    long size) throws
  • So what should we do in particular for
    RawMemoryAccess?

16
java.nio.charset
  • Package java.nio.charset.spi
  • Service-provider classes for the java.nio.charset
    package
  • Package java.nio.charset
  • Encoders and Decoders for difference character
    sets

17
  • //convert ISO-8859-1 encoded bytes in a
    ByteBuffer to a string in a CharBuffer and visa
    versa
  • // Create the encoder and decoder for ISO-8859-1
  • Charset charset Charset.forName("ISO-8859-1")
    CharsetDecoder decoder charset.newDecoder()
  • CharsetEncoder encoder charset.newEncoder()
  • try
  • ByteBuffer bbuf encoder.encode(CharBuffer.wra
    p("a string"))
  • CharBuffer cbuf decoder.decode(bbuf) String
    s cbuf.toString()
  • catch (CharacterCodingException e)

18
java.nio.channels
  • Selector provider in .channels.spi provides the
    abstract class by which the Java virtual machine
    maintains a single system-wide default provider
    instance
  • The provider provides instances of
    DatagramChannel, Pipe, Selector,
    ServerSocketChannel, and SocketChannel

19
Selector
Selectable Channel
Selectable Channel
register
Selection Key
20
Selectors
  • A selector maintains three sets of selection keys
  • The key set
  • The selected-key set
  • The cancelled-key
  • Event driven block for events on selection
  • Selection is done by select(), selectNow() and
    select(int timeout). Returns the selected key set.

21
Selection Keys
  • A selection key is created each time a channel is
    registered with a selector
  • A selection key contains two operation sets
  • Interest Set
  • Ready Set

22
Socket Channels
  • Only one operation is valid for
    ServerSocketChannles SelectionKey.OP_ACCEPT
  • SocketChannels support connecting, reading, and
    writing, so this method returns
    (SelectionKey.OP_CONNECT SelectionKey.OP_READ
    SelectionKey.OP_WRITE)

23
Concurrency and NIO
  • Selectors are thread safe but selection keys are
    not
  • Care must be taken to avoid deadlock
  • To use NIO you dont have to be brilliant at
    socket programming but you have to know your
    concurrency and race-condition resolution
    strategies

24
Concurrency Example
  • If you
  • Add a Channel
  • Cancel a channel
  • (3) Modify the interestOps
  • for a channel
  • The selector MUST be awake.
  • If it is blocked on a select(),
  • you will also block.

Selectable Channel
select()
25
Solving concurrency issues
  • Issuing a wakeUp() to nudge the selector
  • This method is similar to notify() but it is a
    queued notify
  • Queuing requests so that on unblocking of the
    selector, all required ops are added
  • It is a better idea to use one thread to wait on
    a selector

26
This Presentation
  • What NIO provides
  • A short tutorial-like introduction to NIO
  • On NIO and patterns for distributed systems
  • Why NIO is important to us

27
A new platform for Scalable Distributed Computing
Development
  • Previously, distributed Java applications were
    not scalable when using RMI or sockets
  • Technologies like JINI provide scalable
    architectures but not scalable servers per se
    the goals are different.

28
Distributed Patterns with NIO
  • Reactor Pattern
  • Implemented in a couple of days
  • Most time spent on fixing concurrency
  • Leaders followers

29
NIO and ACE
  • Similar idea
  • ACE reactor pattern also includes timers. NIO
    does not but includes anything that can be
    described as selectable (eg. pipes)
  • How you implement the pattern is up to you

30
This Presentation
  • What NIO provides
  • A short tutorial-like introduction to NIO
  • On NIO and patterns for distributed systems
  • Why NIO is important to us

31
DRTSJ and RMI
  • Seeks to extend the RTSJ with a real-time RMI for
    development of Distributed RT
  • 2 approaches Non-distributed and distributed
    thread model
  • The engineering of any approach is hard

32
TAO, ACE and RMI
  • We are giving a good look at TAO and ACE and the
    approach taken in RT-CORBA
  • How to migrate some aspect of these with RMI

33
The problems with RMI
  • There is no concept of a POA
  • There is no way to configure an acceptance
    mechanism
  • Real-time systems cannot allow arbitrary
    threading policies
  • JSR 78 Custom Remote References was a step in the
    right direction but was rejected

34
RMI and NIO
  • Currently it is hard (or impossible) to implement
    a scalable RMI using the standard JDK
  • Suns RMI implementation creates a thread for
    each connection but then tries to use those
    connections intelligently
  • Strictly speaking you only need one connection
    per end-point to any endpoint
  • In real-time, this is not going to work
  • Zen developers took up Half-Sync Half-Async
    pattern as it is the only thing that Java would
    offer

35
A new scalable RMI
  • Make use of NIO to handle connections
  • Use a thread-pool rather than create a new thread
    for every connection
  • Still no application control if to be conformant
    with the spec

36
That new scalable RMI and the RTSJ
  • Scoped memory will make it hard. In particular
  • How should scoped memory be used in any
    acceptance and handling pattern
  • How does this effect how applications would have
    to be developed
  • Perhaps some patterns are just inappropriate in
    the presence of scopes?

37
Conclusion
  • Useful and long awaited addition to Java
  • Will make message-passing applications more
    scalable
  • Will make RMI implementations more scalable
  • However, will still be useless for RT unless
    appropriate steps are taken in RMI
Write a Comment
User Comments (0)
About PowerShow.com