1.在写nio的例子时,服务端采用线程池处理请求,遇到一个full gc问题,下面给代码贴出来。
nioserver端代码
package com.nio.study;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 类NIOServer.java的实现描述:TODO 类实现描述
*
* @author macun 2015年11月29日 下午4:18:46
*/
public class NIOServer {
private static final int DEFAULT_PORT = 8888;
private static Selector selector;
private Object lock = new Object();
private Boolean isStart = false;
private ExecutorService threadPool = null;
public void init() throws IOException {
selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
ServerSocket ss = serverChannel.socket();
ss.bind(new InetSocketAddress(DEFAULT_PORT));
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
threadPool = Executors.newFixedThreadPool(10);
// threadPool = new ThreadPoolExecutor(10, 10,
// 1000L, TimeUnit.MILLISECONDS,
// new ArrayBlockingQueue<Runnable>(100));
synchronized (lock) {
isStart = true;
}
System.out.println("nio server init successful.");
}
public void run() throws IOException {
synchronized (lock) {
if (!isStart) {
try {
init();
} catch (IOException e) {
throw new IOException("nio server init failed.");
}
}
}
while (