Download Java NIO - Washington University in St. Louis

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java NIO
Andrew Borg
Andrew Borg. The University of York.
1
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
 Nonblocking IO in RMI
the RTSJ
Andrew Borg. The University of York.
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
Andrew Borg. The University of York.
3
What is Java NIO?
Stands
for New IO.
Started off as a JSR under Sun’s JCP (JSR51).
“APIs for scalable I/O, fast buffered binary and
character I/O, regular expressions, charset
conversion, and an improved filesystem interface.”
Andrew Borg. The University of York.
4
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;
Andrew Borg. The University of York.
5
…and

An API for character-set converters, including a serviceprovider 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
Andrew Borg. The University of York.
6
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
Andrew Borg. The University of York.
7
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
Andrew Borg. The University of York.
8
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
Andrew Borg. The University of York.
9
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
Andrew Borg. The University of York.
10
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
Andrew Borg. The University of York.
11
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 <= mark <= position <= limit <= capacity

Andrew Borg. The University of York.
12
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
Andrew Borg. The University of York.
13
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);
Andrew Borg. The University of York.
14
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?
Andrew Borg. The University of York.
15
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
Andrew Borg. The University of York.
16
//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.wrap("a
string"));
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
} catch (CharacterCodingException e)
{ }
Andrew Borg. The University of York.
17
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
Andrew Borg. The University of York.
18
Selector
Selectable Channel
Selectable Channel
register
Selection Key
Andrew Borg. The University of York.
19
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.
Andrew Borg. The University of York.
20
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
Andrew Borg. The University of York.
21
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)
Andrew Borg. The University of York.
22
Concurrency and NIO
Selectors are thread safe but selection
keys are not
 Care must be taken to avoid deadlock
 To use NIO you don’t have to be brilliant at
socket programming – but you have to
know your concurrency and race-condition
resolution strategies

Andrew Borg. The University of York.
23
Concurrency Example
Selectable Channel
If you:
(1) Add a Channel
(2) 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.
select()
Andrew Borg. The University of York.
24
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
Andrew Borg. The University of York.
25
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
Andrew Borg. The University of York.
26
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.
Andrew Borg. The University of York.
27
Distributed Patterns with NIO

Reactor Pattern:
 Implemented
in a couple of days
 Most time spent on fixing concurrency

Leaders followers
Andrew Borg. The University of York.
28
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
Andrew Borg. The University of York.
29
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
Andrew Borg. The University of York.
30
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
Andrew Borg. The University of York.
31
TAO, ACE and RMI

We are giving a good look at TAO and
ACE and the approach taken in RTCORBA

How to migrate some aspect of these with
RMI
Andrew Borg. The University of York.
32
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
Andrew Borg. The University of York.
33
RMI and NIO

Currently it is hard (or impossible) to implement a scalable RMI
using the standard JDK

Sun’s 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
Andrew Borg. The University of York.
34
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
Andrew Borg. The University of York.
35
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?
Andrew Borg. The University of York.
36
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

Andrew Borg. The University of York.
37