java http服务器 线程池,基于线程池的TCP服务器(JAVA实现)

基于线程池的TCP服务器(JAVA实现)

在开始的tcp/ip socket中 服务端实现方式是一个客户端一个线程,但是,每个新的线程都会消耗系统资源。随着线程数的增加。线程将消耗越来越多的系统资源。

对于这个问题,使用线程池限制线程总数并重复使用线程可以避免这个问题,

具体做法是,在服务器启动时创建一个有固定线程数量组成的线程池,当一个新的客户端连接请求连入服务器时,将由线程池中的一个线程处理,该线程处理完这个客户端之后又返回线程池,继续等待下一次请求,如果当客户端请求到达服务器时。线程池中所有的线程都已经被占用那么把请求放入等待队列中,直到有空闲的线程可用,当然使用线程池并不是最优的解决方案,因为在实际情况中需要考虑线程池的个数,使线程池的大小既不会创建太多,也不会太少,这是很难做到的。这需要一个策略来实现,具体实现后面会有

基于线程池的TCP服务器是实现步骤

1、与一个客户端,一个线程服务器一样,线程池服务器首先创建一个ServerSocket实例。

2、然后创建N个线程,每个线程反复循环,从(共享的)ServerSocket实例接收客户端连接。当多个线程同时调用一个ServerSocket实例的accept()方法时,它们都将阻塞等待,直到一个新的连接成功建立,然后系统选择一个线程,为建立起的连接提供服务,其他线程则继续阻塞等待。

具体实现代码如下:

客户端代码:

public static void tcpClient(String hostname, int port) throws IOException {

String serverResponse = null;

//与端口进行连接

Socket tcpClient = new Socket(hostname, port);

tcpClient.setSoTimeout(6000);

//获取键盘输入

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

//输出流

PrintStream out = new PrintStream(tcpClient.getOutputStream());

//输入流 ,获取服务器消息

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(tcpClient.getInputStream()));

//使用循环主要是为了更好的展示线程池,如果只是建立一次连接就结束,没有必要使用循环

boolean flag = true;

while (flag) {

System.out.print(“输入需要发送的信息:”);

String msg = input.readLine();

//发送数据到服务端

out.println(msg);

try {

serverResponse = bufferedReader.readLine();

System.out.println(“服务端返回:”+serverResponse);

} catch (SocketTimeoutException e) {

System.out.println(“连接超时,无响应”);

}

if (“bye”.equals(msg)) {

flag = false;

}

}

if (tcpClient != null) {

tcpClient.close(); //只关闭socket,其关联的输入输出流也会被关闭

}

}

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

服务端线程类:

public class TcpServerThread implements Runnable {

private Socket client = null;

public TcpServerThread(Socket client) {

this.client = client;

}

public static void executeClient(Socket client) {

String msg = “你好,客户端”;

try {

//获取Socket的输出流,用来向客户端发送数据

PrintStream out = new PrintStream(client.getOutputStream());

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));

boolean flag = true;

while (flag) {

String str = bufferedReader.readLine();

if (str == null || “”.equals(str)) {

flag = false;

} else {

out.println(“服务端消息:” + msg);

System.out.println(“接受到客户端消息客户端消息:” + str);

}

}

out.close();

bufferedReader.close();

client.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void run() {

executeClient(client);

}

}

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

服务端代码

private static final int THREAD_POOL_SIZE = 3;

public static void tcpServerByPool(int serverPort)throws Exception{

final ServerSocket server = new ServerSocket(serverPort);

for(int i = 0; i< THREAD_POOL_SIZE; i++){

Thread thread = new Thread(){

public void run(){

while(true){

try {

//等待客户端的连接

Socket client = server.accept();

TcpServerThread.executeClient(client);

} catch (IOException e) {

e.printStackTrace();

}

}

}

};

thread.start();

}

}

———————

作者:Sean_zzZ

来源:CSDN

原文:https://blog.csdn.net/github_38151745/article/details/79061222

版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值