A Java multi-thread instance

Source: Internet
Author: User

With regard to multithreading, the principles provided on the network are very simple. It is nothing more than inheriting the thread class or implementing the runable interface. However, when I first processed it, it was difficult to understand it, for example, how the program supplements the tasks to be processed and how to enter the specific execution of multithreading.

Of course, because I implemented it on the basis of a senior engineer, many architecture problems do not need to be considered. With the underlying foundation, it is convenient to develop multi-threaded code at the upper layer. Since I do not understand it very well, I can only briefly explain the ideas and discuss them together.

First, solve the problem of multi-threaded task Source:

List <Task> tasks = dao. query ("select task from Task task where task. taskType = 'attachment' and task. finishTime is null "}

The solution here is to create a table, read all the tasks to be processed, and read them from the table when the program is executed (the finishTime has a value after the task is executed, so the difference between a task to be processed and a task to be processed is whether the finishTime is empty)

All pending tasks will be added to the java. util. concurrent. ThreadPoolExecutor of the thread pool.

Then there is the specific program execution process.

(Task. getFinishTime ()! === Work () Exception;

In this way, if you have any new multi-threaded task, you only need to inherit the class above and then implement your own work method.

 

Of course, some trivial problems need to be solved during the entire process, and this is solved by yourself. The overall idea is probably like this, hoping to help you understand multithreading.

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.