计算机领域中Task和Job的区别

There are only two hard things in Computer Science: cache invalidation and naming things (计算科学中只有两件事最难:命名和缓存失效)    —— Phil Karlton

本文就是讨论一个命名的问题。作为开发者,我们经常看到Task和Job这两个词,而他们的中文翻译都是“任务”,很难分清这两者的区别,所以作者针对这两个概念搜集了一些资料,然后结合个人的一些经验,尝试来探讨这个话题。

英汉词典解释

首先看看两者的英文解释(Mac上的英汉・汉英词典,只看两者的名词解释)

task

task |tɑːsk, American tæsk|

.noun
任务 rènwu
to perform or carry out a task
    执行任务
a thankless task
    费力不讨好的差事
a Herculean task
    艰苦卓绝的任务
to take or hold [somebody] to task (about or for or over [something])
    (因某事)训斥某人

job(由于job的解释过多,对例子进行了删减)

job |dʒɒb|

A.noun
①(post)工作 gōngzuò
to have a job 有工作
②(piece of work)活儿 huór
  to have a job for [somebody] (to do) 有活儿给某人(做)
③(matter)事情 shìqing
the job in hand 手头的事情
④(assignment)任务 rènwu to have the job of doing [something]
⑤(result of work)成果 chéngguǒ
a good/poor/lovely job干得不错/干得很糟/招人喜欢的活儿
⑥(duty)职责 zhízé
it's [somebody's] job to do [something] 该由某人负责做某事
⑦(function)作用 zuòyòng
to have the job of doing [something] 具有做某事的作用
⑧colloquial (situation)情况 qíngkuàng
it's a good job (that) ... especially British 幸好…
⑨(difficult activity)难做的事 nán zuò de shì
a real job 很费力的事情
⑩colloquial (object)东西 dōngxi
the car was a fast-looking job 那辆车看上去跑得很快
⑪colloquial (crime)犯罪行为 fànzuì xíngwéi [尤指盗窃或抢劫]
to do a job 行窃
⑫Computing [作为单元处理的] 作业 zuòyè
you need to cancel all pending print jobs 你得取消所有待打印任务

可以看出,job的使用范围远远大于task,但是没有任务的解释。好吧,那我们看看中文任务的解释(Mac汉语词典)。

任务 rènwù 
名 受委派担负的工作或责任。

任务=工作/责任 ???

所以在日常生活中,job是可以替代task的,task可以部分替代job,但是这并没有回答我们的疑问,那么task和job的区别到底是什么呢。

*解释

我们去*看看怎么说。首先是task

In computing, a task is a unit of execution or a unit of work. The term is ambiguous; precise alternative terms include process, light-weight process, thread (for execution), step, request, or query (for work). In the adjacent diagram, there are queues of incoming work to do and outgoing completed work, and a thread pool of threads to perform this work. Either the work units themselves or the threads that perform the work can be referred to as “tasks”, and these can be referred to respectively as requests/responses/threads, incoming tasks/completed tasks/threads (as illustrated), or requests/responses/tasks.

这里直接了当的说明了task的含义比较模糊,它有很多近义词,比如进程、线程、步骤、请求、查询等等。

然后我们来看看Job的解释

In computing, a job is a unit of work or unit of execution (that performs said work). A component of a job (as a unit of work) is called a task or a step (if sequential, as in a job stream). As a unit of execution, a job may be concretely identified with a single process, which may in turn have subprocesses (child processes; the process corresponding to the job being the parent process) which perform the tasks or steps that comprise the work of the job; or with a process group; or with an abstract reference to a process or process group, as in Unix job control.

Jobs can be started interactively, such as from a command line, or scheduled for non-interactive execution by a job scheduler, and then controlled via automatic or manual job control. Jobs that have finite input can complete, successfully or unsuccessfully, or fail to complete and eventually be terminated. By contrast, online processing such as by servers has open-ended input (they service requests as long as they run), and thus never complete, only stopping when terminated (sometimes called “canceled”): a server’s job is never done.

第一句几乎和task解释一模一样,这里提到job的一个组件称之为task或者step。另外提到一点是job一般会有一个最终状态,包括完成(成功或者失败)、未完成、被终止等等。

从*的解释来看,我们基本上可以认为task更多的侧重运行状态,而job则是一个目标,更侧重于完成状态。一个job通常包括多个task。

techtarget解释

后来我google了一下,techtarget也有两者的一些解释,摘录一部分如下

task的解释

In computer programming, a task is a basic unit of programming that an operating system controls. Depending on how the operating system defines a task in its design, this unit of programming may be an entire program or each successive invocation of a program. Since one program may make requests of other utility programs, the utility programs may also be considered tasks (or subtasks). All of today’s widely-used operating systems support multitasking , which allows multiple tasks to run concurrently, taking turns using the resources of the computer.

这个解释基本上把task等同于进程/线程了。

job的解释

In certain computer operating systems, a job is the unit of work that a computer operator (or a program called a job scheduler) gives to the operating system. For example, a job could be the running of an application program such as a weekly payroll program. A job is usually said to be run in batch (rather than interactive) mode. The operator or job scheduler gives the operating system a “batch” of jobs to do (payroll, cost analysis, employee file updating, and so forth) and these are performed in the background when time-sensitive interactive work is not being done. In IBM mainframe operating systems (MVS, OS/390, and successors) a job is described with job control language (JCL). Jobs are broken down into job steps. An example of a job step might be to make sure that a particular data set or database needed in the job is made accessible.

这里job的解释主要是针对操作系统领域的,而且还简单归纳了两者的相似之处。(task通常表示交互式的任务,job表示后台自动化任务)

A similar term is task, a concept usually applied to interactive work. A multitasking operating system serving one or more interactive users can at the same time perform batch jobs in the background.

实际案例&总结

为了更好的理解这两个概念的区别,那我们找几个计算机领域的例子看一看。

首先是task,它用在哪些地方呢

  • windows下的任务管理器,管理系统的进程。
  • linux的进程表结构task_struct,记录进程的信息。
  • Java等编程语言中的各种Task类,偏向描述执行过程。

然后是job(通过搜索可以发现计算机领域的job使用频率远远低于task,和日常使用刚好相反)

  • 操作系统中的Job,作业(job)是计算机操作者(或是一个叫做作业调度器的程序)交给操作系统的执行单位。
  • Hadoop中的Job,和操作系统的job概念类似,是用户编写的一个程序,读取输入数据完成计算之后输出结果。
  • quartz中的Job,org.quartz.Job类,类似于一个Runner,包含execute方法。另外数据库的一行记录也叫Job。

通过之前词典的解释和这些案例,可以得到下面这些结论。

  • job和task是近义词,侧重点不同,很多场景中可以互换。
  • task常用于多任务系统,task之间存在资源竞争,job则常常用于批处理系统,表示用户的一组任务。
  • task通常表示一个过程(进程),job通常表示用户提交的一个任务(目标),期望明确的结果,job可以包含多个task,可以认为task是一个JobHandler
  • 在操作系统领域,job通常指后台批处理任务,而task则指用户正在使用的一个进程。

参考资料

  • https://en.wikipedia.org/wiki/Job_(computing)
  • https://en.wikipedia.org/wiki/Task_(computing)
  • https://whatis.techtarget.com/definition/task
  • https://searchdatacenter.techtarget.com/definition/job
上一篇:IfcQuantityWeight


下一篇:ckeditor粘贴word文档图片的代码