📄 writerhandler.java
字号:
/**
* Created at Nov 10, 2008
*/
package com.jdev.net.event;
import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;
/**
* <p>Title: WriterHandler</p>
* <p>Description: </p>
* @author Lawrence
* @version 1.0
*/
public class WriterHandler implements Runnable {
private static List<SelectionKey> pool = new LinkedList<SelectionKey>();
private static Notifier notifier = Notifier.getNotifier();
/**
*
*/
public WriterHandler() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
while (!Thread.interrupted()) {
try {
SelectionKey key;
synchronized (pool) {
while (pool.isEmpty()) {
pool.wait();
}
key = (SelectionKey) pool.remove(0);
}
// 处理写事件
write(key);
} catch (Exception e) {
continue;
}
}
}
/**
* 处理向客户发送数据
* @param key SelectionKey
*/
public void write(SelectionKey key) {
SocketChannel sc = (SocketChannel) key.channel();
try {
Response response = new Response(sc);
// 触发onWrite事件
notifier.fireOnWrite((Request) key.attachment(), response);
// // 关闭
// // 触发onClosed事件
// notifier.fireOnClosed((Request) key.attachment());
// sc.finishConnect();
// sc.socket().close();
// sc.close();
} catch (Exception e) {
notifier.fireOnError("Error occured in Writer: " + e.getMessage());
}
}
/**
* 处理客户请求,管理用户的联结池,并唤醒队列中的线程进行处理
*/
public static void processRequest(SelectionKey key) {
synchronized (pool) {
pool.add(pool.size(), key);
pool.notifyAll();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -