在JAVA中如何实现长时间任务(一)

2014-11-23 23:22:34 · 作者: · 浏览: 0
By Serguei Eremenko, Builder.com | Thursday,November 21 2002 12:00 PM.


摘要:
在软件开发中,我们经常面临着处理长时间任务的多线程 编程问题。在我们的ezOne平台的
开发中就多处涉及到,如JPC数据服务JPC数据处理服务 报警联动 门禁系统等。本人在编
写DEMO程序的过程中几易其稿,煞费心机,但依然感觉有许多地方需要改进,为了减少多
线程编程带来的风险,我尝试翻译整理了一个类似问题的解决方案框架以达到一劳永逸。

为了便于 阅读,保留原文。引用请保留作者和文章来源.

关键词:
Thread、 Lock 、 Notification、长时间任务。


一、问题背景
Quite often, you need a class to perform tasks like data processing, listening
to events, or checking another class activities during the application’s
lifetime. To achieve this, you probably use threads with a set of locks and
notifications. Java Thread API is well documented, but you need a great deal
of code and experience to make your thread work properly and efficiently. You
can avoid writing such classes from scratch every time you need them and build
a more robust application by applying the framework well discuss in this
article.

在应用程序中我们经常需要一个类去完成像数据处理、监听事件或检查另一个类的活动等
任务。为了达到这个目标,我们可能使用带有一套锁和消息通知的线程。JAVA 线程API已
经很好的文档化,但为了使线程能够正确而高效地运行,程序员仍然需要丰富的编程经验
并编写大量的代码。通过应用本篇文章中讨论的框架,程序员能够避免忍受煎熬写大量的
代码,快速创建健壮的应用程序。
二、长时间运行任务的程序框架
Framework for long-running tasks
The primary thing about a long-lived task is that it should somehow be kept
running during the application lifetime. The right way to accomplish this is
to provide a thread of execution for a particular task. You create a task as a
thread or as an implementation of the java.lang.Runnable interface. If you
implement Runnable, you can gain better object-oriented design and avoid the
single-inheritance problems. You can also more efficiently manipulate with
Runnable instances, for example, using a thread pool that usually needs a
Runnable instance, not a thread, to run.

关于长时间运行的任务的主要事情是如何在应用程序的生命期使它一直保持运行。实现的
恰当方法是提供一个线程来执行这个特定的任务。我们可以通过继承Thread类或实现
java.lang.Runnable接口来达到该目标。如果采用实现Runnable接口的方式,就可以能够
获得更好的面向对象的设计,同时可以避免JAVA中的单继承问题。另外,我们也能更有效
的处理Runnable实例(例如使用线程池通常需要一个Runnable实例而不是线程来运行)。

The essence of the framework is the abstract class Worker ( Listing A), which
implements the Runnable interface and provides the helper methods for
efficient task handling. Some of the methods are fully implemented, like the
run() method, but some are abstract and have to be filled by you. If you want
to create a long-running class, you need only to extend the Worker class and
implement several abstract methods. Let’s look at these methods in more
detail.

框架的基础是一个叫Worker的抽象类,它实现了Runnable接口,并提供了有效处理任务的
好方法。这些方法有些已经被实现,如run()方法,但有些是抽象方法,开发人员必须自己
来实现。如果要创建一个长时间运行的类,你只需要继承Worker类并实现几个抽象方法。
让我们看看这些方法的细节。

The run() method of the Worker class is designed to continuously execute the
work() method until it is stopped. The work() method can be responsible for
data processing, reaction to some event, file reading or writing, SQL
execution, etc. It can throw an exception, so it is a good practice to
propagate it and let the run() method handle it.

Worker 类的run()方法被设计成只要不停止运行就持续的执行work()方法。work()方法可
以负责数据处理、事件响应、文件读写、,执行SQL命令等操作。这样work()方法能够抛出
异常,并将异常传给run(),然后由run()方法来