⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclientthread.java

📁 java多线程设计模式中的线程池设计模式源码
💻 JAVA
字号:
 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -