AIO问题请帮忙解答一下,为什么会阻塞掉

问题描述:
1. 先启动服务端进入等待
2. 启动客户端进行数据发送,然后服务端与客户端线程都会被阻塞

这里是服务端代码:

package org.morningdew.javaaio.main;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Server {
    public static void main(String[] args) {
        AsynchronousServerSocketChannel assc = null;
        try {
            assc = AsynchronousServerSocketChannel.open();
            assc.bind(new InetSocketAddress(9090));
            System.out.println("server start at 9090 !");
            while(true) {
                System.out.println("Server waiting connect !");
                Future<AsynchronousSocketChannel> f = assc.accept();
                AsynchronousSocketChannel asc = f.get();
                System.out.println(Thread.currentThread().getName());
                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                byteBuffer.clear();
                StringBuilder stringBuilder = new StringBuilder();
                while(asc.read(byteBuffer).get() > 0) {
                    byteBuffer.flip();
                    byte[] b = new byte[byteBuffer.remaining()];
                    byteBuffer.get(b);
                    byteBuffer.clear();
                    stringBuilder.append(new String(b, "utf-8"));
                }
                System.out.println(stringBuilder.toString());
                ByteBuffer byteBufferWrite = ByteBuffer.allocate(1024);
                byteBufferWrite.clear();
                byteBufferWrite.put("我服务点".getBytes("utf-8"));
                byteBufferWrite.flip();
                while(byteBufferWrite.hasRemaining()) {
                    System.out.println("写出成功");
                    asc.write(byteBufferWrite).get();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            try {
                if(assc != null && assc.isOpen()) {
                    assc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


下是客户端代码:



package org.morningdew.javaaio.main;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;

public class Client {
    public static void main(String[] args) {
        AsynchronousSocketChannel asc = null;
        try {
            asc = AsynchronousSocketChannel.open();
            asc.connect(new InetSocketAddress(9090)).get();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.clear();
            byteBuffer.put("我是客户端".getBytes("utf-8"));
            byteBuffer.flip();
            while(byteBuffer.hasRemaining()) {
                asc.write(byteBuffer).get();
            }
            ByteBuffer byteBufferRead = ByteBuffer.allocate(1024);
            byteBufferRead.clear();
            StringBuilder stringBuilder = new StringBuilder();
            System.out.println("等到接受服务端数据");
            while(asc.read(byteBufferRead).get() > 0) {
                byteBufferRead.flip();
                byte[] b = new byte[byteBufferRead.remaining()];
                byteBufferRead.get(b);
                byteBufferRead.clear();
                stringBuilder.append(new String(b, "utf-8"));
            }
            System.out.println(stringBuilder.toString());
            System.out.println("zhixing wanbu ");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            try {
                if(asc != null && asc.isOpen()) {
                    asc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}