socketclientthread.java
来自「java多线程设计模式中的线程池设计模式源码」· Java 代码 · 共 73 行
JAVA
73 行
package S7.A6;
import java.net.*;
import java.io.*;
public class SocketClientThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter=0;
private int id=counter++;//线程id,表示线程的标号
private static int threadcount=0;//当前的线程数
final int port=8888;//服务器监听的端口号
public static int threadCount()
{
return threadcount;
}
public SocketClientThread(InetAddress addr){
System.out.println("Making client:"+id);
threadcount++;//线程数统计器+1
try{
socket=new Socket(addr,port);
}
catch(IOException e)
{ }
try{
}
catch(IOException e)
{
try{
socket.close();
}
catch(IOException e2){}
}//end of try
}//end of constructor SocketClientThread(InetAddress addr)
public void run(){
try{
for (int i=0;i<25;i++)
{
out.println("Client:"+id+":"+i);
String str=in.readLine();
System.out.println(str);//在控制台上打印从输入流接收的字串
}//end of for
out.println("END");
}
catch(IOException e)
{
}
finally{
try{
socket.close();
}
catch(IOException e)
{ }
threadcount--;
}//end of try
}// end of run() method
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?