多线程
题目
将基于TCP协议的Client-Sever通信程序示例的服务端程序改造成多线程版
源码
整理后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import java.io.*; import java.net.*;
class MyThread extends Thread { private Socket clientSocket;
MyThread(Socket client) { clientSocket = client; }
public void run() { try { InputStream inStream = clientSocket.getInputStream(); OutputStream outStream = clientSocket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream);
String line = null; while ((line = in.readLine()) != null) { System.out.println("Thread Number:"+ this.getName()); System.out.println("Message from this client:" + line); out.println(line); out.flush(); } }catch (Exception e) { System.out.println("Exception"); return; } finally { try { clientSocket.close(); } catch (Exception e){ e.printStackTrace(); return; } } } }
public class Server { public static void main(String[] args) throws Exception { Socket clientSocket = null; ServerSocket listenSocket = new ServerSocket(8189);
System.out.println("Server listening at 8189"); while(true) { clientSocket = listenSocket.accept(); System.out.println("Accepted connection from client"); MyThread t = new MyThread(clientSocket); t.start(); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.io.*; import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
String userInput = null; String echoMessage = null;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Socket socket = new Socket("127.0.0.1", 8189); System.out.println("Connected to Server");
InputStream inStream = socket.getInputStream(); OutputStream outStream = socket.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream);
while(!(userInput=stdIn.readLine()).equals("quit")) { out.println(userInput); out.flush(); echoMessage = in.readLine(); System.out.println("Echo from server: " + echoMessage); }
socket.close();
} }
|
线程池
题目
将基于TCP协议的Client-Sever通信程序示例的服务端程序改造成线程池版
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
import java.io.*; import java.net.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;
class MyThread implements Runnable{ private Socket clientSocket;
MyThread(Socket client) { clientSocket = client; }
public void run() { try { InputStream inStream = clientSocket.getInputStream(); OutputStream outStream = clientSocket.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream);
String line = null; while ((line = in.readLine()) != null) { System.out.println("Message from this client:" + line); out.println(line); out.flush(); } }catch (Exception e) { System.out.println("Exception"); return; } finally { try { clientSocket.close(); } catch (Exception e){ e.printStackTrace(); return; } } } }
public class Server { public static void main(String[] args) throws Exception { Socket clientSocket = null; ServerSocket listenSocket = new ServerSocket(8189);
ThreadPoolExecutor executor = new ThreadPoolExecutor(6,12,100, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); System.out.println("Server listening at 8189"); while(true) { clientSocket = listenSocket.accept(); System.out.println("Accepted connection from client"); MyThread t = new MyThread(clientSocket); executor.execute(t); } } }
|