Java多线程服务器一直是我们不断的研究和创新的技术,下面我们就来看看如何才能更好的使用相关服务。们的Java多线程服务器研究完了。在我们接着讨论带有连接池示例之前,让我们回顾一下创建和使用“多线程版”的服务器的步骤:
修改 acceptConnections() 以用缺省为 50(或任何您想要的大于 1 的指定数字)实例化 ServerSocket 。
修改 ServerSocket 的 handleConnection() 以用 ConnectionHandler 的一个实例生成一个新的 Thread 。
借用 RemoteFileServer 的 handleConnection() 方法的代码实现 ConnectionHandler 类。
附: MultithreadedRemoteFileServer 的完整代码清单
Java代码
- import java.io.*;
- import java.net.*;
- public class MultithreadedRemoteFileServer {
- protected int listenPort;
- public MultithreadedRemoteFileServer(int aListenPort) {
- listenPort = aListenPort;
- }
- public void acceptConnections() {
- try {
- ServerSocket server = new ServerSocket(listenPort, 5);
- Socket incomingConnection = null;
- while (true) {
- incomingConnection = server.accept();
- handleConnection(incomingConnection);
- }
- }catch (BindException e) {
- System.out.println("Unable to bind to port " + listenPort);
- } catch (IOException e) {
- System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
- }
- }
- public void handleConnection(Socket connectionToHandle) {
- new Thread(new ConnectionHandler(connectionToHandle)).start();
- }
- public static void main(String[] args) {
- MultithreadedRemoteFileServer server = new MultithreadedRemoteFileServer(3000);
- server.acceptConnections();
- }
- }
- import java.io.*;
- import java.net.*;
- public class MultithreadedRemoteFileServer {
- protected int listenPort;
- public MultithreadedRemoteFileServer(int aListenPort) {
- listenPort = aListenPort;
- }
- public void acceptConnections() {
- try {
- ServerSocket server = new ServerSocket(listenPort, 5);
- Socket incomingConnection = null;
- while (true) {
- incomingConnection = server.accept();
- handleConnection(incomingConnection);
- }
- } catch (BindException e) {
- System.out.println("Unable to bind to port " + listenPort);
- } catch (IOException e) {
- System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
- }
- }
- public void handleConnection(Socket connectionToHandle) {
- new Thread(new ConnectionHandler(connectionToHandle)).start();
- }
- public static void main(String[] args) {
- MultithreadedRemoteFileServer server = new MultithreadedRemoteFileServer(3000);
- server.acceptConnections();
- }
- }
ConnectionHandler 的完整代码清单
Java代码
- import java.io.*;
- import java.net.*;
- public class ConnectionHandler implements Runnable {
- protected Socket socketToHandle;
- public ConnectionHandler(Socket aSocketToHandle) {
- socketToHandle = aSocketToHandle;
- }
- public void run() {
- try {
- PrintWriter streamWriter = new PrintWriter(socketToHandle.getOutputStream());
- BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
- String fileToRead = streamReader.readLine();
- BufferedReader fileReader = new BufferedReader(new FileReader(fileToRead));
- String line = null;
- while ((line = fileReader.readLine()) != null)
- streamWriter.println(line);
- fileReader.close();
- streamWriter.close();
- streamReader.close();
- } catch (Exception e) {
- System.out.println("Error handling a client: " + e);
- }
- }
- }
以上就是对Java多线程服务器相关使用的介绍。希望大家有所收获。
【编辑推荐】