【初心者】Java質問・相談スレ45【大歓迎】
■ このスレッドは過去ログ倉庫に格納されています
0528デフォルトの名無しさん
NGNG非同期時の送信処理は以下の方法で大丈夫でしょうか?
こういう風にした方が良い、これは必要が無い、というのがあったら教えてください。
// 送信データをlinkedListに追加
public void write(ByteBuffer buf) throws IOException {
synchronized(linkedList) {
linkedList.addLast(buf.duplicate());
}
socketChannel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
}
// 実際の送信処理(Selectorスレッドから呼ばれる)
public void writable() throws IOException {
ByteBuffer buf = null;
synchronized(linkedList) {
while(true) {
if(linkedList.isEmpty()) {
socketChannel.register(selector, SelectionKey.OP_READ);
return;
} else {
buf = (ByteBuffer)linkedList.getFirst();
socketChannel.write(buf);
if(buf.hasRemaining())
return;
linkedList.removeFirst();
}
}
}
}
■ このスレッドは過去ログ倉庫に格納されています