What scenarios in a Java Web project will use Java multithreading?

Source: Internet
Author: User
Tags java web tomcat

Q: Can you give a brief description of the scenarios you need to use multithreaded programming in Java Web development?

Some understanding of multithreading, but not very clear specific application scenarios, you can simply say you encounter the multithreaded programming scene?

Answer one:

The most typical example is:
1, the user registration completes sends the gift package/the integral and so on, and the integral and so on is another system and the time consuming, and this kind of task even if the failure is not particularly important.
2, Background thread: such as regular implementation of special tasks, such as regular updates to the configuration files, task scheduling (such as quartz), some monitoring for regular information collection.

Answer two:

The most typical applications, such as Tomcat,tomcat internal multithreading, hundreds of clients to access the same Web application, tomcat access after the subsequent processing to throw to a new thread to deal with, this new thread finally calls to our servlet program, such as Doget or Dopost methods.

Without multithreading, when hundreds of people access a Web application at the same time, Tomcat will have to queue up for serial processing, so that the client simply cannot tolerate that access speed.

There is the need for asynchronous processing, the need to use multithreading. For example, task A and task B are processed in parallel, and a single thread can only be serially processed, task A is done first and then task B. If you want multiple tasks to execute at the same time, you must assign a thread to each task, and then schedule the threads of the Java virtual machine to perform multiple jobs at the same time. For example, if your CPU is multiple cores, you can have a CPU execute a thread. If there is only one CPU, the bottom is based on the principle of time-sharing reuse, each thread according to the time slice to obtain CPU resources.

answer three:

Particularly time-consuming operations, such as backing up a database, you can open a thread to perform a backup, and then perform the return, and the foreground continues to query the background for thread execution status


Q: What scenarios in the Java project need to use multithreading, deeply confused, please use the master guidance.

Answer:

Scene One: A business logic has many times the cycle, there is no impact between each cycle, such as verifying the existence of 10,000 URL paths, the normal situation to cycle 10,000 times, each to verify each URL, so the efficiency will be very low, assuming that the verification of a need for 1 minutes, a total of 1 0 minutes, a little scary. At this point, you can use multithreading, 10,000 of the URL into 50 equal parts, open 50 threads, no thread only to verify 200, so that all the thread execution is far less than 1 0 minutes.


Scenario Two: You need to know the progress of a task, for example, we often see the progress bar, the implementation can be in the task of adding an integer attribute variable (so that different methods can be shared), the task execution to a certain extent to the variable value plus 1, another open a thread at intervals to access this variable, and feedback to the user.


In short, using multithreading is to make full use of CPU resources, improve program execution efficiency, when you find a business logic implementation efficiency is particularly low, time-consuming, you can consider using multithreading. However, the CPU to execute which thread of the time and order is uncertain, even if set the priority of the thread, so the risk of using multithreading is relatively large, there will be many unexpected problems, must be more familiar with the concept, many constructs different scenes to test to be able to master!


Related Article

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.