Talking about the application of multithreading in Java program

Source: Internet
Author: User

In a highly concurrent web site, multithreading is essential. The following first talk about the role of multithreading in the program.
1, improve the response speed of front-end requests. When we execute a more time-consuming method, the HTTP request does not respond or even times out, and we can use multithreading to process a more time-consuming method if the data is allowed to be delayed on the business. This way the front-end sends a request, and the backend enables a thread to process the task without blocking the main thread.
2, reduce the pressure of the server. This includes our web containers, such as Tomcat, jetty, etc., as well as database servers. Because we use multi-threading, and the thread pool size is limited, such as 30, then the link to request the database is limited to 30, that is, the ability to execute the method at the same time only 30 threads, the rest of the task is placed in our thread's task queue, so that the database will not be overwhelmed by the sudden request. Of course, to alleviate the database pressure, it is more recommended to use Message Queuing, using Message Queuing we can save data for batch submission, and just use multi-threading, it is not good to implement the process of saving data.
3, improve processing capacity, increase performance, make full use of server resources. If we are going to load three tables of data into the cache, the simplest of us is to open a single thread, and a total of three threads to load the cache is much more efficient than using a thread to traverse the three tables next to each other.

Of course, multithreading brings us benefits, there are a lot of places to pay attention to
1, since we use multi-threading, then we are not so strict with the real-time data, including the return value of the method, data warehousing operations. Therefore, the use of multi-threaded methods must be well logging, because an exception occurred, the front end is not feedback. Of course, multithreading can also have a return value, you can return to the future, the inside of the Get method can be blocked until the thread execution is complete.
2, I think the most annoying is, in order to improve performance, the program must use multi-threaded or other asynchronous operations, but also must ensure that the user experience, such as user modification operation, this modification operation we are asynchronous, require the user at the front end must also immediately see the result of the modification. This requires both the front and back end to work at the same time.
3, very important point is the problem of thread safety, such as we on a set of data modification, in multi-threaded high concurrency, if the processing is not good, there may be some unexpected results. Even if we are not using multi-threading, thread safety is something we have to pay attention to in high concurrency situations. For example, when we were in the stress test, when the number of landings hit 10w, when we read the page list, we were always holding some strange anomalies, but the number of people was less, or we were testing alone, there would be no anomalies. Check to see, it turns out that we used a page plug-in to determine whether a collection is empty, if NULL is initialized operation, because this set is a global variable, so many people, there will be a thread security problem.
4, for their own unsure how many threads of operation, must use the thread pool, otherwise not only improve performance, but will degrade performance, and even lead to the JVM memory full.
5, for the thread pool size problem, there are many articles explaining these issues, what is the distinction between IO or CPU-intensive, I think it is not necessary to be so strict to calculate a number, test to achieve their own requirements can be.

It says a bunch of messy theories, and here's how the thread pool is used in a Java program.
1, can be directly used to create the new Threadpoolexecutor (), but this method is cumbersome, if there is no special requirements, it is not recommended to use this method

2, using the Executors class provided by Java to create a thread, we are more commonly used, such as:
[Java] View plain copy
Executorservice Executor=executors.newfixedthreadpool (10www.yongshiyule178.com/);//This method creates a thread pool with a size of 10 and no bounds for the task queue , in fact, the method is also called Method 1. Other types of threads can also be created in executors, such as Executors.newcachedthreadpool (), which are not described here executors specific uses.

3, Spring also provides us with a more convenient method of initializing thread pool, of course, it is also called the method of 1. Here's how to use it:
[Java] View plain www.haiyuanylpt.com copy
<!--The following configuration is added to the spring scan configuration file, a thread pool of size 5 is declared--
< Task:executor id= "addnotic" pool-size= "5" www.thd1956.com//>
In Java programs, we inject through @autowired or @resource, such as
[Java] View plain copy
@Resource (name= "www.xinbeiyule.cn/addnotic")
Private Threadpooltaskexecutor Addnotic_executor;

Because we use spring for our project, we generally use the method in method 3 for thread pooling, which is very convenient.

Talking about the application of multithreading in Java program

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.