??????????????嵥 9 ?????
?????嵥 9. ???????
????maxMemoryValue=67108864
????reservedMemoryValue=0
???????? NIO ?????????????????????????????????? JDK NIO ????????? Apache ?? Mina??JBoss ?? Netty??Sun ?? Grizzly ??????Щ???????????????? TCP ?? UDP Э?飬???? Netty ?????? NIO ??????????? Web ?????????????????????? Web ??????
????Java AIO
????AIO ??????????
????java.nio.channels.AsynchronousChannel???????? Channel ????? IO ??????
????java.nio.channels.AsynchronousServerSocketChannel??ServerSocket ?? AIO ?汾?????? TCP ??????????????????????
????java.nio.channels.AsynchronousSocketChannel???????????? Socket Channel?????????????
????java.nio.channels.AsynchronousChannelGroup???? Channel ???????????????????????????? AsynchronousChannelGroup ???????????????????????????????? IO ???????? CompletionHandler??AsynchronousServerSocketChannel ?????????????????? AsynchronousChannelGroup???????? AsynchronousServerSocketChannel ?????? AsynchronousSocketChannel ???????????飬?????????
????java.nio.channels.CompletionHandler???? IO ???????????????????????? IO ??????????????????????AIO ?? API ????????????????????????????????? Future ????????? CompletionHandler??????? CompletionHandler ????????Щ handler ????????? AsynchronousChannelGroup ??????????????????????С??????????????
????????????????????????????? AIO ?????????
?????嵥 10. ????????
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
public class SimpleServer {
public SimpleServer(int port) throws IOException {
final AsynchronousServerSocketChannel listener =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));
//????????????????? Handle ???????
listener.accept(null?? new CompletionHandler<AsynchronousSocketChannel?? Void>() {
public void completed(AsynchronousSocketChannel ch?? Void att) {
listener.accept(null?? this);// ?????????????
handle(ch);// ??????????
}
@Override
public void failed(Throwable exc?? Void attachment) {
// TODO Auto-generated method stub
}
});
}
public void handle(AsynchronousSocketChannel ch) {
ByteBuffer byteBuffer = ByteBuffer.allocate(32);//????? Buffer
try {
ch.read(byteBuffer).get();//???????
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byteBuffer.flip();
System.out.println(byteBuffer.get());
// Do something
}
}
?????嵥 11. ????????
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class SimpleClientClass {
private AsynchronousSocketChannel client;
public SimpleClientClass(String host?? int port) throws IOException??
InterruptedException?? ExecutionException {
this.client = AsynchronousSocketChannel.open();
Future<?> future = client.connect(new InetSocketAddress(host?? port));
future.get();
}
public void write(byte b) {
ByteBuffer byteBuffer = ByteBuffer.allocate(32);
System.out.println("byteBuffer="+byteBuffer);
byteBuffer.put(b);//?? buffer д???????????
byteBuffer.flip();
System.out.println("byteBuffer="+byteBuffer);
client.write(byteBuffer);
}
}
?????嵥 12.Main ????
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
public class AIODemoTest {
@Test
public void testServer() throws IOException?? InterruptedException {
SimpleServer server = new SimpleServer(9021);
Thread.sleep(10000);//???????????????????????????????????????
}
@Test
public void testClient() throws IOException?? InterruptedException?? ExecutionException {
SimpleClientClass client = new SimpleClientClass("localhost"?? 9021);
client.write((byte) 11);
}
public static void main(String[] args){
AIODemoTest demoTest = new AIODemoTest();
try {
demoTest.testServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
demoTest.testClient();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
???????????????????????????? AIO ??????????????????????
??????????
????I/O ?? NIO ???????????????????????? I/O ????????????????????????????????????????????? NIO ???????????????????????????????????????????????????? NIO ????????????????????????????????????????????? AIO ???????????? I/O??NIO??AIO ??????????????????????????????????????????????????????????????????????????????????