計算機領域中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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章