Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger.
Original article, reproduced please specify the author: jmppok and origin: http://blog.csdn.net/jmppok/article/details/44590991.
Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger.
Original article, reproduced please specify the author: jmppok and origin: http://blog.csdn.net/jmppok/article/details/44590991.
AsyncTask is a Java asynchronous invocation framework written by individuals to support users:
1) Customize Task, and set Task's type (Type), subType (subType), time out (TImeout), flag (Flag - can be used to distinguish different Tasks), Task's input parameters (input), etc.
2) Submit to the framework for asynchronous execution through submitTask. The framework finds the corresponding Task Exectuor and executes with multi-threads.
3) TaskExecutor can be customized and added to the framework through configuration. TaskExecutor supports Execotor Chain, and multiple Executors can be combined to execute sequentially. It also supports real-time notification of Task status and progress during Task execution.
4) Users can use Task Collector to query all Tasks through Task Manager. Task Collector supports combinatorial queries by Task Id, Task Type, Task SubType, Task State, Task Flag, Task beginTIme, Task finishTime, etc.
5) Supporting persistence, user-submitted Task s can be stored in data base Medium. Even if Task is interrupted during execution, it will resume execution after restarting from the database.
6) Users can get the reference ITaskReference of Task through the query interface, and the status and Progress of Task can be obtained in real time through ITaskReference.
7) The user can define the FinishedCallBack callback of Task, which is passed in at Submit Task and automatically callback after Task is completed.
8) Support users to use it synchronously through waitForTask of ITaskReference.
9) Users can obtain Task execution results or error information through ITaskReference.
Code: https://git.oschina.net/jmpp/AsyncTask
1. Why do you need AsyncTask? Is it different from Asyn4J?
1.1 Java's traditional Thread and Runable functionality is inadequate
Java Thread, ThreadPool and other multi-threaded programming interfaces are provided. But these are all basic interfaces, although easy to use, but the function is relatively simple, many scenarios can not meet the needs.
For example, the following scenarios:
1) I need to submit a task, change the task to be executed asynchronously in the background, and at the same time I need to observe the status and progress of the task in real time.
2) When submitting a task, I hope to pass in parameters. When the task is completed, I can actively notify me and get the results.
3) Task persistence, I hope that after the completion of the task, you can query the list of tasks executed. Or the task can be re-executed if it fails.
If these scenarios are to be implemented, Java's own interfaces are obviously not satisfied, and a new framework is needed to implement them.
1.2 Asyn4J
Asyn4J is a similar framework, but it does not currently support tasks such as timeout, persistence, task callback and so on.
2. Design and Implementation
2.1 Interface Design
Direct drawing
2.2 Code Implementation
See Specific Implementation Code Git@OSChttps://git.oschina.net/jmpp/AsyncTask
The code structure is as follows:
Here is a brief description of the realization of ideas:
1) The whole implementation is based on Java Thread and ThreadPool, without using a third-party framework.
2) Persistence is based on MySQL There is only one database table task, see tasks.sql.
3) The implementation of persistence layer uses Mybatis, which gives Mybatis a code generation tool and directly generates the corresponding task table. data structure.
4) Object serialization is necessary for persistence. Kryo is used here. Why Kryo? See my other article: Summary of Java Object Serialization.
5) Log4j is used in the log.
3. test
Specifically visible code: https://git.oschina.net/jmpp/AsyncTask
3.1 Custom MyTask
3.2 Custom MyTaskExecutor
3.3 Configuration of MyTaskExecutor
Add in taskexecutors.properties:
It's actually the type of task = Executor of task.
3.4 Submit Task and monitor progress in real time
3.5 Terminate Task execution and restart the program for Task recovery testing
Or the same code as 3.4
1) Task is not available for the first run, and a Task is submitted. submitAndWaitTask();
2) If the change Task is terminated without being executed, the Task will be restored after the second boot.
3) At this point query Running Tasks will query the running Task and progress to the branch waiting for the Task.
4) Of course, if you stop for a long time before restarting, you will find that Task timeout.
4. deficiency
1. The overall implementation is relatively simple, especially in the database table to store Java object serialization fields, lazy varchar(2000), may exceed, preferably blob. (Why didn't you use Blob at that time, because of laziness, if Blob, mybatis generated more complex code, there will be a XXwithBlob, inconvenient calls....)
2. The number of thread pools is dead and should be configurable.
3.test It's relatively simple. There may be unknown bug s.