任务与服务进行数据库操作 [英] Task vs Service for database operations

查看:53
本文介绍了任务与服务进行数据库操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX 8任务和服务之间有什么区别?在哪种情况下最好使用一个?在数据库操作中最好使用什么?

What is the difference between JavaFX 8 Task and Service and in which case is it better to use one over the other? What is better to use in database operations?

推荐答案

任务和服务之间的主要区别-一次执行与重复执行

任务是一次性的事情-您只能使用一次任务.如果要再次执行相同的任务,则需要构造一个新的Task实例.

A Task is a one off thing - you can only use a Task once. If you want to perform the same Task again, you need to construct a new Task instance.

服务具有可重用的接口,因此您可以多次启动和重新启动单个服务实例.在幕后,只需将任务"定义作为输入并根据需要创建新任务.

A Service has a reusable interface so that you can start and restart a single service instance multiple times. Behind the scenes, it just takes a Task definition as input and creates new tasks as needed.

用例示例

任务示例 =>监视和报告长期运行的启动任务在应用程序初始化方面的进度,例如启动页面"示例.

Task Example => monitoring and reporting progress of a long running startup task on application initialization, like this Splash Page example.

服务示例 => 内部加载程序实现用于WebEngine ,其中需要为加载的每个页面重复执行同一任务,即异步加载页面.

Service Example => The internal load worker implementation for WebEngine where the same task, loading a page asynchronously, needs to be repeated for each page loaded.

建议-最初尝试仅使用任务而不使用服务来解决您的问题

除非您对JavaFX中的并发性更加熟悉,否则我建议您坚持只使用Task而不是Service.任务的界面稍微简单一些.您只需在需要时创建新的Task实例即可完成服务的大部分工作.如果在了解Task之后,发现自己想要启动或重新启动Task的预定义API,则可以在那时开始使用Service.

Until you are more familiar with concurrency in JavaFX, I'd advise sticking to just using a Task rather than a Service. Tasks have a slightly simpler interface. You can accomplish most of what a Service does simply by creating new Task instances when you need them. If, after understanding Task, you find yourself wanting a predefined API for starting or restarting Tasks, then start using Service at that time.

使用任务的数据库访问示例

任务或服务都可以在JavaFX应用程序线程之外执行数据库操作.使用哪个取决于您的个人编码偏好以及正在执行的特定数据库操作.

Either Task or Service will work for performing database operations off of the JavaFX application thread. Which to use depends on your personal coding preference as well as the particular database operation being performed.

这里是一个使用通过JDBC访问数据库的任务的示例.该示例是为 JavaFX-SQL查询的后台线程创建的.

Here is an example which uses a Task to access a database via JDBC. The example was created for JavaFX - Background Thread for SQL Query.

背景信息

JavaFX并发教程提供了以下方面的良好概述:任务和服务.

The JavaFX concurrency tutorial provides a good overview of Task and Service.

任务中有出色的文档和服务 javadoc,包括示例代码,例如案例.

There is excellent documentation in the Task and Service javadoc, including sample code for example use cases.

工作人员,任务和服务定义(来自Javadoc)

任务和服务都是 Workers ,所以他们有一个共同点:

Task and Service are both Workers, so they have this in common:

Worker是一个对象,它在一个或多个后台线程中执行某些工作,并且其状态是可观察的并且对JavaFX应用程序可用,并且可以从JavaFX Application主线程使用.

A Worker is an object which performs some work in one or more background threads, and whose state is observable and available to JavaFX applications and is usable from the main JavaFX Application thread.

任务定义:

FutureTask的完全可观察的实现.任务公开了其他状态和可观察的属性,这些属性和属性对于JavaFX中的异步任务编程很有用. .由于服务旨在执行任务,因此任何任务 由应用程序或库代码定义的代码可以轻松地与 服务.

A fully observable implementation of a FutureTask. Tasks expose additional state and observable properties useful for programming asynchronous tasks in JavaFX . . Because Service is designed to execute a Task, any Tasks defined by the application or library code can easily be used with a Service.

服务定义:

服务是封装信息的非可视组件 需要在一个或多个后台线程上执行某些工作.作为 该服务是JavaFX UI库的一部分,它了解JavaFX 应用程序线程,旨在缓解应用程序 开发人员从管理交互的多线程代码的负担中 与用户界面.因此,所有的方法和状态 服务应专门从JavaFX调用 应用程序线程.

A Service is a non-visual component encapsulating the information required to perform some work on one or more background threads. As part of the JavaFX UI library, the Service knows about the JavaFX Application thread and is designed to relieve the application developer from the burden of managing multithreaded code that interacts with the user interface. As such, all of the methods and state on the Service are intended to be invoked exclusively from the JavaFX Application thread.

Service实现了Worker.因此,您可以观察 后台操作并有选择地取消它.服务是可重用的 Worker,表示可以重置和重新启动.因此, 服务可以声明式构建,并可以按需重新启动.

Service implements Worker. As such, you can observe the state of the background operation and optionally cancel it. Service is a reusable Worker, meaning that it can be reset and restarted. Due to this, a Service can be constructed declaratively and restarted on demand.

这篇关于任务与服务进行数据库操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆