91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA NIO ServerSocketChannel(線程池版)

發布時間:2020-07-24 13:32:19 來源:網絡 閱讀:2534 作者:楓葉還沒紅 欄目:軟件技術

服務器端:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
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.LinkedList;
import java.util.List;

public class ServerSocketThreadPool{
	private static final int MAX_THREAD = Runtime.getRuntime().availableProcessors();
	private ThreadPool pool = new ThreadPool(MAX_THREAD);

	private static int PORT_NUMBER = 1234;

	public static void main(String[] args) throws Exception {
		new ServerSocketThreadPool().go();

	}

	public void go() throws Exception {
		int port = PORT_NUMBER;
		System.out.println("Listenning on port:" + port);
		// 創建通道 ServerSocketChannel
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		// 綁定監聽端口
		serverSocketChannel.socket().bind(new InetSocketAddress(port));
		// 設置為非阻塞方式
		serverSocketChannel.configureBlocking(false);
		// 創建選擇器
		Selector selector = Selector.open();

		// 通道注冊到選擇器
		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		
		while (true) {
			// 一直阻塞,直到有數據請求
			int n = selector.select();
			if (n == 0) {
				continue;
			}
			Iterator<SelectionKey> it = selector.selectedKeys().iterator();
			while (it.hasNext()) {
				SelectionKey key = it.next();
				if (key.isAcceptable()) {
					ServerSocketChannel server = (ServerSocketChannel) key.channel();
					SocketChannel socket = server.accept();
					registerChannel(selector,socket, SelectionKey.OP_READ);
					sayHello(socket);
				}
				if (key.isReadable()) {
					readDataFromSocket(key);
				}
				it.remove();
			}

		}

	}
	
	public void registerChannel(Selector selector,SelectableChannel channel,int ops)throws Exception{
		if(channel==null){
			return;
		}
		channel.configureBlocking(false);
		channel.register(selector, ops);
		
	}
	
	public void sayHello(SocketChannel socket) throws Exception{
		ByteBuffer buffer=ByteBuffer.allocate(1024);
		buffer.clear();
		buffer.put("hello client".getBytes());
		buffer.flip();
			socket.write(buffer);
	}

	public void readDataFromSocket(SelectionKey key) throws Exception {
		WorkThread thread=pool.getWork();
		if(thread==null){
			return;
		}
		thread.serviceChannel(key);
	}

	private class ThreadPool {
		List idle=new LinkedList();
		

		public ThreadPool(int poolSize) {
			for(int i=0;i<poolSize;i++){
				WorkThread thread=new WorkThread(this);
				thread.setName("worker"+(i+1));
				thread.start();
				idle.add(thread);
			}

		}
		public WorkThread getWork(){
			WorkThread thread=null;
			synchronized (idle) {
				if(idle.size()>0){
					thread=(WorkThread) idle.remove(0);
					
				}
			}
			return thread;
		}

		public void returnWorker(WorkThread workThread) {
			synchronized (idle) {
				idle.add(workThread);
			}
		}

	}

	private class WorkThread extends Thread {
		private ByteBuffer buffer = ByteBuffer.allocate(1024);
		private ThreadPool pool;
		private SelectionKey key;

		public WorkThread(ThreadPool pool) {
			this.pool = pool;
		}

		public synchronized void run() {
			System.out.println(this.getName() + " is ready");
			while (true) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
					this.interrupt();
				}
				if (key == null) {
					continue;
				}
				System.out.println(this.getName() + " has been awaken");
				try{
					drainChannel(key);
				}catch(Exception e){
					System.out.println("caught '"+e+"' closing channel");
					try{
						key.channel().close();
					}catch(IOException ioe){
						ioe.printStackTrace();
					}
					key.selector().wakeup();
				}
				key=null;
				this.pool.returnWorker(this);

			}

		}
		synchronized void serviceChannel(SelectionKey key){
			this.key=key;
			key.interestOps(key.interestOps()&(~SelectionKey.OP_READ));
			this.notify();
		}
		
		void drainChannel(SelectionKey key)throws Exception{
			SocketChannel channel=(SocketChannel) key.channel();
			buffer.clear();
			int count;
			while((count=channel.read(buffer))>0){
				buffer.flip();
				/*while(buffer.hasRemaining()){
					channel.write(buffer);
				}*/
				byte[] bytes;
				bytes=new byte[count];
				buffer.get(bytes);
				System.out.println(new String(bytes));
				buffer.clear();
			}
			if(count<0){
				channel.close();
				return;
			}
			key.interestOps(key.interestOps()|SelectionKey.OP_READ);
			key.selector().wakeup();			
		}

	}

}

客戶端:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class ClientTest {
	
	private static SocketChannel socketChannel;
	private static Selector selector;
	public ClientTest()throws Exception {
		socketChannel=SocketChannel.open();
		socketChannel.connect(new InetSocketAddress("127.0.0.1", 1234));
		socketChannel.configureBlocking(false);
		selector=Selector.open();
		socketChannel.register(selector, SelectionKey.OP_READ);
	}
	public static void main(String[] args) throws Exception{
		ClientTest test=new ClientTest();

		
		ByteBuffer buffer=ByteBuffer.allocate(1024);
		buffer.put("hello server".getBytes());
		buffer.flip();
		while(buffer.hasRemaining()){
			test.socketChannel.write(buffer);
		}
		
		buffer.clear();
		socketChannel.socket().shutdownOutput();
		
		String response=receiveData(test.socketChannel);
		System.out.println(response);
	}
	
	public static String receiveData(SocketChannel socketChannel2) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String response = "";
		try {
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			byte[] bytes;
			int count = 0;
			while ((count = socketChannel2.read(buffer)) >= 0) {
				buffer.flip();
				bytes = new byte[count];
				buffer.get(bytes);
				baos.write(bytes);
				buffer.clear();
			}

			bytes = baos.toByteArray();
			response = new String(bytes);
		} finally {
			try {
				baos.close();
			} catch (Exception ex) {
			}
		}
		return response;
	}

	
	

	

}

更詳細關于ServerSocketChannel的線程池的理解,請參考《JAVA NIO》這本書的第四章選擇器。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

且末县| 丹巴县| 亳州市| 沙田区| 双流县| 武乡县| 枝江市| 临高县| 肥乡县| 大庆市| 栾城县| 定襄县| 榆社县| 墨竹工卡县| 宜宾市| 中宁县| 嵊州市| 墨脱县| 磐石市| 普格县| 罗山县| 九龙城区| 闽侯县| 大邑县| 土默特左旗| 南宁市| 麦盖提县| 辉南县| 平定县| 铅山县| 清新县| 革吉县| 西林县| 邵东县| 进贤县| 敦化市| 杭锦后旗| 鲜城| 镇康县| 井研县| 巴林左旗|