- 我们先看一个最古老的多线程实现的服务端
public class SocketThread { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9889); while (true) { Socket client = serverSocket.accept(); new Thread(() -> { try { System.out.println("client port :" + client.getPort()); InputStream inputStream = client.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); while (true) { System.out.println(bufferedReader.readLine()); } } catch (IOException e) { e.printStackTrace(); } }).start(); } } }
strace指令查看:
- 我们用【strace】指令检测一下:
-o:输出 到某个路径
-ff: 抓取这个程序所有线程
[root@bogon thread]# strace -ff -o /root/data/thread-group/ooxx java SocketThread //程序对内核有没有发生什么系统调用
然后可以再thread-group目录中看到线程详情:
jps进行查看
- 首先用【jps】指令查看一下进程详情:
[root@bogon thread-group]# jps 12485 Jps 12475 Loop
- 然后我们进入【Loop】进程,继续深入:
[root@bogon thread-group]# cd /proc/12475 [root@bogon 12475]# ls attr comm fd map_files net pagemap schedstat statm wchan autogroup coredump_filter fdinfo maps ns patch_state sessionid status auxv cpuset gid_map mem numa_maps personality setgroups syscall cgroup cwd io mountinfo oom_adj projid_map smaps task clear_refs environ limits mounts oom_score root stack timers cmdline exe loginuid mountstats oom_score_adj sched stat uid_map
1》然后我们进入【task】目录,可以看到线程详情:
[root@bogon 12475]# cd task/ [root@bogon task]# ls 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 [root@bogon task]# ll total 0 dr-xr-xr-x. 7 root root 0 May 27 10:08 12475 dr-xr-xr-x. 7 root root 0 May 27 10:08 12476 dr-xr-xr-x. 7 root root 0 May 27 10:08 12477 dr-xr-xr-x. 7 root root 0 May 27 10:08 12478 dr-xr-xr-x. 7 root root 0 May 27 10:08 12479 dr-xr-xr-x. 7 root root 0 May 27 10:08 12480 dr-xr-xr-x. 7 root root 0 May 27 10:08 12481 dr-xr-xr-x. 7 root root 0 May 27 10:08 12482 dr-xr-xr-x. 7 root root 0 May 27 10:08 12483 dr-xr-xr-x. 7 root root 0 May 27 10:08 12484
2》也可以进入【fd】目录
以数字为文件描述符名字的文件
- 我们新开一个界面,可以用【nc】模拟一次客户端请求:
[root@bogon ~]# nc 192.168.33.10 9889
此时可以看到连接情况的变化:
服务端会打印出客户端连接进入的信息(此处我重新连了一下,所以和上述端口会产生不一致):
1》客户端发送:
2》服务端接收: