The introduction and simple realization of thread pool

Source: Internet
Author: User

It is common for server programs to use threading technology to respond to customer requests, and you may think this is an efficient way to do it, but have you ever thought about optimizing the threading approach. This article will show you how server programs use line pool to optimize performance and provide a simple thread pool implementation.

The technical background of the thread pool

In object-oriented programming, it is time-consuming to create and destroy objects because creating an object takes up memory resources or other additional resources. This is especially true in Java, where the virtual machine will attempt to track each object so that it can be garbage collected after the object is destroyed. So one way to improve the efficiency of the service program is to minimize the number of objects created and destroyed, especially the creation and destruction of resource-consuming objects. How to use the existing objects to serve is a key problem to be solved, in fact, this is the reason why some "pooling resources" technology arises. For example, everyone familiar with the database connection pool is based on this idea, this article will introduce the thread pool technology is also consistent with this idea.

At present, some well-known large companies are particularly optimistic about the technology, and has long been in their products to apply the technology. For example, IBM's Websphere,iona Orbix 2000 in Sun Jini, Microsoft's MTS (Microsoft Transaction Server 2.0), COM + and so on.

Do you also want to apply the technology to the server program now?

How thread pooling technology improves the performance of server programs

I refer to Server programs as programs that can accept customer requests and process requests, not just those that accept requests from network clients.

Multithreading technology mainly solves the problem of multiple threads executing in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit. However, improper application of multithreading increases the processing time for individual tasks. You can give a simple example:

Assume that the time to complete a task on a single server is T

T1 创建线程的时间
   T2 在线程中执行任务的时间,包括线程间同步所需时间
   T3 线程销毁的时间

Obviously t = t1+t2+t3. Note that this is an extremely simplified assumption.

We can see that T1,T3 is the cost of multithreading itself, we are eager to reduce the time spent on T1,T3, thus reducing t time. However, some thread users do not notice this, so frequently create or destroy threads in the program, which causes T1 and T3 to occupy a considerable proportion of T. Obviously this is highlighting the thread's weaknesses (T1,T3), not the benefits (concurrency).

Thread pooling technology is focused on how to shorten or adjust the t1,t3 time technology, thereby improving server program performance. It arranges t1,t3 at the start and end of a server program, or some idle time, so that there is no t1,t3 overhead when a server program processes a client request.

The thread pool not only adjusts the time period that T1,T3 produces, but it also significantly reduces the number of threads created. Looking at an example:

Suppose a server handles 50,000 requests a day, and each request requires a separate thread to complete. We compare the total number of threads generated when processing these requests using the thread pooling technology and servers that are not good for thread pooling technology. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number of threads in the thread pool or the upper limit (hereinafter referred to as the thread pool size), and the total number of threads is 50000 if the server does not use the thread pool to process these requests. The general thread pool size is far less than 50000. Therefore, server programs that utilize thread pools do not waste time processing requests in order to create 50000, thereby increasing efficiency.

These are assumptions that do not adequately explain the problem, and I will discuss the simple implementation of the thread pool and test the program to illustrate threading technology benefits and application areas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.