简体   繁体   English

用Java创建健壮的多线程可伸缩应用程序时应考虑哪些问题?

[英]What are the issues to be considered while creating a Robust Multi-threaded Scalable application in Java?

I am planning to build a stand-alone / web based Java application. 我打算构建一个独立的/基于Web的Java应用程序。 It should be capable of handling thousands of requests at a moment. 它应该能够同时处理数千个请求。 Request messages come in through a TCP port say 6040. I have a dedicated thread to constantly listen for messages from clients. 请求消息通过TCP端口(例如6040)进入。我有一个专用线程来不断侦听来自客户端的消息。 Once the message is read from the socket this thread will spawn a thread to service a request. 从套接字读取消息后,该线程将产生一个线程来处理请求。 So its basically one thread per request. 因此,基本上每个请求一个线程。

Servicing the request involves many database operations like calling stored procedures in remote databases, performing updates/insertions in local database and also logging. 服务请求涉及许多数据库操作,例如在远程数据库中调用存储过程,在本地数据库中执行更新/插入以及记录日志。 A response should be sent to the client after servicing the request. 服务请求后,应将响应发送给客户端。

I hope the scenario is clear. 我希望情况是清楚的。 In short I want to build a robust multithreaded scalable application (to use the cliche). 简而言之,我想构建一个健壮的多线程可伸缩应用程序(以使用陈词滥调)。 My questions are as follows: 我的问题如下:

  1. Should the main thread listening to incoming requests spawn a thread for each request or should it just take one from a thread pool. 侦听传入请求的主线程应该为每个请求生成一个线程,还是应该仅从线程池中获取一个。 What I'm asking is should I use Thread Spawning or Thread Pooling in my application. 我要问的是我应该在应用程序中使用线程生成还是线程池。
  2. Is it possible to deploy a standalone java application on clusters? 是否可以在集群上部署独立的Java应用程序?
  3. Is it a good idea to deploy and run it with the web-application (deployed on JBoss 7.1 server), which I intend to develop for administration purposes or should it be a standalone application? 我打算出于管理目的而开发该Web应用程序(在JBoss 7.1服务器上部署)上部署和运行它是一个好主意,还是应该是一个独立的应用程序?
  4. In general what are the issues to be considered while building a Robust Multi-threaded Scalable applications in java 通常,在Java中构建健壮的多线程可伸缩应用程序时要考虑哪些问题?

To ensure that your Java application should scale, there's basically one simple rule to follow: Don't share state. 为了确保Java应用程序能够扩展,基本上要遵循一个简单的规则:不要共享状态。

All state should be local to the thread or in the backing datastore. 所有状态应在线程本地或在后台数据存储中。 This ensures that you can spin up unlimited copies of your application (on the same machine or not) that are entirely independent, and use a simple load balancer in front. 这样可以确保您可以旋转完全独立的应用程序的无限制副本(无论是否在同一台计算机上),并在前面使用简单的负载平衡器。

To answer your thread question specifically, it's almost always better to use an appropriately configured thread pool. 要专门回答您的线程问题,使用适当配置的线程池几乎总是更好。 You avoid the overhead of spawning the thread inline to your request and you can configure reasonable limits to avoid resource contention / exhaustion. 您可以避免在请求中内联产生线程的开销,并且可以配置合理的限制来避免资源争用/耗尽。

Consider using Threadpools rather than spawning a Thread when the request arrives to save some response time. 考虑使用线程池,而不是在请求到达时生成线程,以节省一些响应时间。 Additionally, you could use non-blocking IO (check New I/O to have a single thread monitor many connections. Also, what Steven Schlansker said: don't share state! 另外,您可以使用非阻塞IO(请选中“ 新建I / O”以使单个线程监视许多连接。此外,史蒂文·史兰斯克说:不要共享状态!

I'm not sure if it really deserves to build it from scratch. 我不确定是否真的应该从头开始构建它。 From productivity perspective, it'll be much quicker to reuse existing web-container, like Jetty, Tomcat, etc. If you follow the Servlet API, you only need to describe your business logic, and container will deal with concurrency for you. 从生产率的角度来看,重用现有的Web容器(如Jetty,Tomcat等)将更快。如果您遵循Servlet API,则只需描述您的业务逻辑,容器即可为您处理并发性。

It takes a lot of time to tune performance of a web-container. 调整Web容器的性能需要花费大量时间。 Even large companies choose Tomcat/Jetty, such as Google's App Engine. 甚至大型公司也选择Tomcat / Jetty,例如Google的App Engine。

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

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