多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 1.创建线程池 ~~~ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 线程池 */ public class HandlerSocketServerPool { // 储存线程池对象 private ExecutorService executorService; /** * * @param maxThreadNum 最大线程池 * @param queueSize 处理任务的个数 * public ThreadPoolExecutor(int corePoolSize, 核心线程数 * int maximumPoolSize,最大线程数量 * long keepAliveTime,线程空闲时间 * TimeUnit unit, 线程空闲时间 单位 * BlockingQueue<Runnable> workQueue) */ HandlerSocketServerPool(int maxThreadNum,int queueSize){ executorService= new ThreadPoolExecutor( 3,maxThreadNum, 120, TimeUnit.SECONDS, // ArrayBlockingQueue 阻塞式队列 泛型任务对象 queueSize 最大任务对象 new ArrayBlockingQueue<Runnable>(queueSize) ); } /** * 提交任务让消息队列缓存起来 * @param target */ public void execute(Runnable target) { // 执行 executorService.execute(target); } } ~~~ # 2.创建任务对象 ~~~ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; /** * 任务对象 */ public class ServerRunnableTarget implements Runnable { private Socket socket; public ServerRunnableTarget(Socket socket){ this.socket=socket; } @Override public void run() { // 从socket中获取字节输入流对象 try (InputStream inputStream = socket.getInputStream()) { //把字节输入流包装成缓存字节输入流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String msg; while ((msg=bufferedReader.readLine())!=null){ System.out.println("服务器收到信息:"+msg); } } catch (Exception e) { e.printStackTrace(); } } } ~~~ # 3.创建服务端 ~~~ import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { System.out.println("服务端启动"); try { // ServerSocket 对象进行服务器端口注册 ServerSocket serverSocket = new ServerSocket(9999); // 初始化线程池 final HandlerSocketServerPool pool = new HandlerSocketServerPool(3, 10); while (true){ Socket socket = serverSocket.accept(); // 把socket交给线程池进行处理 ServerRunnableTarget target = new ServerRunnableTarget(socket); // 提交任务 pool.execute(target); } }catch (Exception e){ e.fillInStackTrace(); } } } ~~~ # 4. 创建客户端 ~~~ import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Client { public static void main(String[] args) { try { // 创建socket链接请求 Socket socket = new Socket("127.0.0.1", 9999); // 从socket中获取字节输出流对象 OutputStream socketOutputStream = socket.getOutputStream(); //把字节输出流包装成打印流 PrintStream printStream = new PrintStream(socketOutputStream); // 扫描器 Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请说:"); printStream.println(scanner.nextLine()); printStream.flush(); } } catch (Exception e) { e.fillInStackTrace(); } } } ~~~