📄 connectionhandler.java
字号:
/** * Example code for JavaOne hands-on J2SE lab * * Concurrency utilities (JSR-166) example **/package threadpool;import java.io.*;import java.net.*;import java.util.concurrent.*;public class ConnectionHandler implements Runnable { private final Socket socket; private final int connectionID; /** * Constructor * * @param socket The socket on which incoming data will arrive **/ public ConnectionHandler(Socket socket, int connectionID) { this.socket = socket; this.connectionID = connectionID; } /** * run method to do the work of the handler **/ public void run() { System.out.println("Connection " + connectionID + ", started"); try { InputStream is = socket.getInputStream(); // Loop to do something with the socket here while (true) { byte[] inData = new byte[100]; /* If the number of bytes read is less than zero then the connection * has been terminated so we end the thread */ if (is.read(inData) < 0) break; System.out.print("[" + connectionID + "]: " + new String(inData)); } } catch (IOException ioe) { // Ignore } System.out.println("Connection " + connectionID + ", ended"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -