NIO双线程处理服务器原型完整源代码(续)

51 篇文章 1 订阅
40 篇文章 0 订阅

增加了一个写线程,这次真的是完整了。

修改了以下文件,其他没变。

 

1、Server

package server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

public class Server {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8080);
  
  try {
   Selector acceptSelector = Selector.open();
   Selector ioSelector = Selector.open();
   
   ServerSocketChannel ssChannel = ServerSocketChannel.open();
   ssChannel.configureBlocking(false);
   ssChannel.socket().bind(address);
   ssChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
   
   AcceptThread acceptThread = new AcceptThread();
   IOThread ioThread = new IOThread();
   WriteThread writeThread = new WriteThread();
   
   ioThread.setSelector(ioSelector);
   ioThread.setThread(writeThread);
   acceptThread.setAcceptSelector(acceptSelector);
   acceptThread.setIOSelector(ioSelector);
   writeThread.setSelector(ioSelector);
   
   
   ioThread.start();
   acceptThread.start();
   writeThread.start();
   
   ioThread.join();
   acceptThread.join();
   writeThread.join();
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }

}

 

2、IOThread

package server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class IOThread extends Thread {
 private Selector selector;
 private WriteThread writeThread;
 
 public void setSelector(Selector selector) {
  this.selector = selector;
 }
 
 public void run() {
  ByteBuffer buffer = ByteBuffer.allocate(1024);
  while(true) {
   int keys = 0;
   try {
    keys = selector.select();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   
   if(keys > 0) {
    Set<SelectionKey> selectedKeys = selector.selectedKeys();
    Iterator<SelectionKey> it = selectedKeys.iterator();
    
    while(it.hasNext()) {
     SelectionKey key = it.next();
     if(key.isReadable()) {
      System.out.println("IO: read");
      SocketChannel channel = (SocketChannel) key.channel();
      writeThread.setChannel(channel);
      try {
       buffer.clear();
       int i = channel.read(buffer);
       System.out.println(i);
       if(i < 0) channel.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      //key.interestOps(SelectionKey.OP_WRITE);
      /*
      try {
       channel.register(selector, SelectionKey.OP_WRITE);
      } catch (ClosedChannelException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      */
     }else if(key.isWritable()) {
      System.out.println("IO: write");
      SocketChannel channel = (SocketChannel) key.channel();
      //key.interestOps(SelectionKey.OP_READ);
      try {
       channel.register(selector, SelectionKey.OP_READ);
      } catch (ClosedChannelException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
     it.remove();
    }
   } 
  }
 }

 public void setThread(WriteThread writeThread) {
  // TODO Auto-generated method stub
  this.writeThread = writeThread;
 }
}

 

3、WriteThread

package server;

import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class WriteThread extends Thread {
 private SocketChannel channel;
 private Selector selector;
 
 public void setChannel(SocketChannel channel) {
  this.channel = channel;
 }

 public void setSelector(Selector selector) {
  this.selector = selector;
 }
 
 public void run() {
  
  while(true) {
   if(channel != null) {
    try {
     channel.register(selector, SelectionKey.OP_WRITE);
    } catch (ClosedChannelException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     break;
    }
    

   } else {
   try {
    this.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
  }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值