java BIO socket 实例二

实例二 java socket通信基于线程池实现,

客户端代码:

package com.my.socket2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketClient {

	public static void main(String agrs[])  {
		
		ByteArrayOutputStream output=null;
		try{
			
			int i=1;
			while(true){
				Socket socket=new Socket();
				System.out.println("start new Thread:"+i);
				new Thread(new ClientThread(socket,i)).start();
				i++;
				Thread.sleep(2000);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(output!=null){
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	static class ClientThread implements Runnable{

		private Socket cSocket;
		private int i;
		
		public ClientThread(Socket cSocket, int i) {
			super();
			this.cSocket = cSocket;
			this.i = i;
		}


		@Override
		public void run() {
			try {
				cSocket.connect(new InetSocketAddress(8088));
				OutputStream out=cSocket.getOutputStream();
				String con="thread"+i+"\r\n";
				out.write(con.getBytes());
				Thread.sleep(1000);
				out.write("quit".getBytes());
				out.close();
				cSocket.close();
				
			} catch (Exception e) {
				try {
					cSocket.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
}

服务端代码

package com.my.socket2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerSocketTest {

	public static void main(String agrs[])  {
		
		try{
			
			ServerSocket socket=new ServerSocket(8088);
			ServerThreadQueue serverThreadQueue=ServerThreadQueue.getInstance();
			ServerThreadPool.getInstance().init();
			
			int i=1;
			while(true){
				Socket cSocket=socket.accept();
				System.out.println("线程数:"+i);
				serverThreadQueue.add(new ServerThread(cSocket));
				i++;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	static class ServerThreadPool {
		
		private int threadNum;
		
		private static ServerThreadPool serverThreadPool=new ServerThreadPool(20);
		
		public static ServerThreadPool getInstance(){
			return  serverThreadPool;
		}
		
		public ServerThreadPool(int threadNum){
			this.threadNum=threadNum;
		}
		
		private void execute(){
			ServerThreadQueue serverThreadQueue=ServerThreadQueue.getInstance();
			ExecutorService service=Executors.newFixedThreadPool(threadNum);
			while(true){
				try{
					Runnable runnable=serverThreadQueue.take();
					service.execute(runnable);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		
		public void init(){
			new Thread(new Runnable() {
				@Override
				public void run() {
					execute();
				}
			}).start();
		}
		
	}
	
	
	static class ServerThreadQueue {
		
		private static ServerThreadQueue serverThreadQueue=new ServerThreadQueue();
		
		private ArrayBlockingQueue<Runnable> queue=new ArrayBlockingQueue<Runnable>(100);

		public static ServerThreadQueue getInstance(){
			return serverThreadQueue;
		}
		
		private Runnable take() throws InterruptedException{
			return queue.take();
		}
		
		private void add(Runnable runnable){
			System.out.println("对列数量:"+queue.size());
			queue.add(runnable);
		}
		
	}
	
	
	static class ServerThread implements  Runnable {
		
		private Socket socket;
		
		public ServerThread(Socket socket){
			this.socket=socket;
		}
		
		public void run() {
		
			try{
				InputStream input=socket.getInputStream();
				BufferedReader reader=new BufferedReader(new InputStreamReader(input));
				
				while(true){
					String line=reader.readLine();
					if(line!=null){
						System.out.println(line);
					}
					
					if(line!=null && line.equals("quit")){
						System.out.println(line);
						socket.close();
						break;
					}
					
				}
				
			}catch(Exception e){
				e.printStackTrace();
				try {
					socket.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			
		}
	}
	
}

实例二通信方式相对于实例一的通信方式优点是服务端的线程数是可以控制的,代码中给定了一个固定大小为20的线程池, jvisualvm中线程上升到31条,就 不在增加了,如图.

java BIO socket 实例二_第1张图片



你可能感兴趣的:(java,socket)