简体   繁体   English

带Web套接字的Java多线程

[英]Java multithreading with web sockets

I have a web application where the front end communicates with the backend through websockets. 我有一个Web应用程序,前端通过websockets与后端通信。 I want to multi thread each request coming from the front end. 我想多线程来自前端的每个请求。 ie; as soon as a socket request comes from the front end, I should create a separate thread for it so that it runs concurrently. 一旦套接字请求从前端发出,我应该为其创建一个单独的线程,以便它可以并发运行。 I have a class that keeps track of each request and delegates the work to other class based on the request parameters. 我有一个跟踪每个请求的类,并根据请求参数将工作委托给其他类。 I want to multi thread the request processing. 我想对请求处理进行多线程处理。 How should I start with it. 我应该如何开始。 I have knowledge about threads but I dont know the right approach to proceed. 我对线程有所了解,但我不知道正确的方法来进行。

Your approach is extremely bad. 您的方法非常糟糕。 I can't stress how bad it is. 我不能强调它有多糟。

you should read about asynchronous IO and find a suitable Java library which supports (real) asynchronous Websockets. 您应该阅读有关异步IO的知识,并找到一个支持(真实)异步Websocket的合适的Java库。

A Thread per request is a recipe for server which can't handle more than few thousands request at a time. 每个请求线程是服务器的配方,服务器一次不能处理数千个请求。 it was a popular approach back in the days, but ditched in favor of asynchronous IO + small threadpool. 在过去,它是一种流行的方法,但放弃了异步IO +小线程池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM