java线程和操作系统线程_操作系统中的多任务和多线程

java线程和操作系统线程

什么是多任务操作系统? (What is Multitasking Operating System?)

When any program is running it is referred to as a task. In a Multitasking Operating System, two or more tasks are active simultaneously.

任何程序在运行时都称为任务。 在多任务操作系统中 ,两个或多个任务同时处于活动状态。

In this, we stop one task temporarily to work one another. It also means that the computer can work with more than one program at a time.

在这种情况下,我们暂时停止一项任务以相互工作。 这也意味着计算机一次可以处理多个程序。

For instance, you can fetch information from one database on the screen analyzing data, while the computer is sorting data from another database, on the other hand performing calculations on a separate worksheet.

例如,您可以在屏幕上从一个数据库中获取信息以分析数据,而计算机正在从另一个数据库中对数据进行排序时,另一方面,您可以在单独的工作表上执行计算。

Multitasking in OS

Another example is, In modern operating systems, we can play MP3 music, edit documents in Microsoft Word, surfing the Google Chrome all are simultaneous.

另一个例子是,在现代操作系统中,我们可以同时播放MP3音乐,在Microsoft Word中编辑文档,浏览Google Chrome。

It is not same as "multiple loading" of applications, also referred to as "context switching" or "task switching," which is what happens when you use MultiFinder or System 7 on the Macintosh or the DOS task switcher.

它不同于应用程序的“多次加载”,也称为“上下文切换”或“任务切换”,当您在Macintosh或DOS任务切换器上使用MultiFinder或System 7时,会发生这种情况。

In Context switching, several applications can be open, but only one application is working at a time. Multitasking based on the time-sharing along with context switching.

在上下文切换中,可以打开多个应用程序,但一次只能运行一个应用程序。 基于分时的多任务处理以及上下文切换。

多任务系统的工作 (Working of Multitasking System)

In a time-sharing system, each process is assigned some specific amount of time for this time slice a process can be executed. For instance, there are 4 processes P1, P2, P3 ready to be executed by the processor. Each of them is assigned some time slice for which they will execute e.g time quantum of 4 nanoseconds (4 ns). When one process begins execution (say P3), it executes for that quantum of time (4 ns). After 4 ns the CPU starts the execution of the other process (say P1) for the specified quantum of time.

在分时系统中,每个进程都为此时间片分配了特定的时间量,可以执行一个进程。 例如,准备有4个进程P1,P2,P3准备由处理器执行。 它们中的每一个都被分配了一定的时间片,将对其执行,例如4纳秒(4 ns)的时间量子。 当一个进程开始执行时(例如P3),它将执行该时间段(4 ns)。 4 ns后,CPU在指定的时间范围内开始执行另一个进程(例如P1)。

By this CPU shares the time between all processes and execute according to the time slice. As soon as time slice of one process expires, another process ready to begin its execution.

由此CPU共享所有进程之间的时间,并根据时间片执行。 一旦一个进程的时间片到期,另一进程就准备开始执行。

Here the concept of context switch is occurring but it is occurring so fast. While jobs or tasks are running the user thinks that those multiple processes/ jobs are executing simultaneously and separately interact with the system.

这里上下文切换的概念正在发生,但是发生得如此之快。 当作业或任务正在运行时,用户认为那些多个进程/作业正在同时执行并分别与系统交互。

But literally, only one process/ job is executing at a particular point of time. In the multitasking system, the concept of time-sharing is introduced because each running process takes only a fair quantum of the CPU time.

但是从字面上看,在特定时间点仅执行一个流程/作业。 在多任务系统中 ,引入了分时的概念,因为每个运行进程仅占用相当一部分CPU时间。

多任务操作系统的类型 (Types of Multitasking Operating System)

  1. True multitasking

    真正的多任务处理

    Two or more tasks are carried out by the Operating system rather than switching from one process to another. This capacity of Operating system is

    操作系统执行两项或多项任务,而不是从一个进程切换到另一个进程。 操作系统的容量为

    true multitasking.

    真正的多任务处理

  2. Preemptive multitasking

    抢先式多任务处理

    Preemptive multitasking is a task in which an operating system uses criteria to make a decision how long to allocate to any one task before giving another task a turn to use the operating system. If the Operating System takes control from one job and giving it to another job, it is called preemption.

    抢占式多任务处理是一种任务,其中操作系统使用标准来决定在分配给一个任务再使用该操作系统之前要分配多长时间。 如果操作系统从一项工作中获得控制权并将其交给另一项工作,则称为抢占。

    A common criterion for preempting is simply is time slicing, this type of system is sometimes called a time-sharing operating system. In some operating systems, some applications have a higher priority than the other applications, It is necessary to give control to the higher priority process or task as soon as they are initiated.

    抢占的一个常见标准就是时间分片,这种类型的系统有时称为分时操作系统。 在某些操作系统中,某些应用程序具有比其他应用程序更高的优先级。有必要在启动它们后立即将控制权交给更高优先级的进程或任务。

什么是多线程操作系统? (What is Multithreading Operating System?)

A flow of control is called a thread. A program is a set of instructions which constitute different parts of a single program. Each different part of the program is referred to as thread of the program.

控制流称为线程。 程序是一组指令,它们构成单个程序的不同部分。 程序的每个不同部分都称为程序的线程。

A process that has only one flow of control is referred to as a single thread.

仅具有一个控制流的进程称为单个线程。

A process having multiple flows of control is referred to as single multithread (heavyweight process).

具有多个控制流的进程称为单个多线程(重量级进程)。

Example: In a word processor, one thread (t1) is for displaying graphics, second thread (t2) for reading keystrokes from the user and a third thread (t3) for performing spelling and grammar checking in the background.

示例:在文字处理器中,一个线程(t1)用于显示图形,第二线程(t2)用于从用户读取击键,而第三线程(t3)用于在后台执行拼写和语法检查。

Multithreading in OS

多线程操作系统的工作 (Working of Multithreading Operating System)

Threads such as child processes that use the parent process resources but execute on its own. But in case of a GUI. If we are performing a calculation on the GUI (which is taking a very long time to finish). Now we can not interact with the rest of the GUI until this command finishes its execution.

诸如子进程之类的线程使用父进程资源但自己执行。 但是如果是GUI。 如果我们正在GUI上执行计算(这需要很长时间才能完成)。 现在,在此命令执行完毕之前,我们无法与GUI的其余部分进行交互。

To be able to interact with the rest of the GUI, this command of calculation should be assigned to another separate thread. So at this point of time, two threads will be executing i.e. one for calculation, and one for the rest of the GUI. Hence for a single process, we use multiple threads for multiple functionalities.

为了能够与GUI的其余部分进行交互,应将此计算命令分配给另一个单独的线程。 因此,此时,将执行两个线程,即一个用于计算,一个用于其余的GUI。 因此,对于单个进程,我们将多个线程用于多种功能。

多线程操作系统的优点 (Advantages of Multithreading Operating System)

  • Responsiveness – It allows a program to continue its execution even if part of it is blocked or is performing a lengthy operation thereby increasing the responsiveness to the user.

    响应速度 –即使部分程序被阻塞或正在执行冗长的操作,它也允许程序继续执行,从而提高了对用户的响应速度。

  • Resource sharing – The memory and the resources of the process shared by threads to which they belong.

    资源共享 –由它们所属的线程共享的进程的内存和资源。

  • Utilization of Multiprocessor Architecture – In Multiprocessor Architecture each thread may be running in parallel on a different processor. Multithreading on a multi-CPU machine increases concurrency.

    多处理器体系结构的利用 –在多处理器体系结构中,每个线程可能在不同的处理器上并行运行。 CPU机器上的多线程可提高并发性。

  • Economy – It is costly to allocate memory and resources for process creation. Threads share resources of the processes to which they belong, it is more economical to create threads.

    经济性 –为过程创建分配内存和资源的成本很高。 线程共享它们所属进程的资源,创建线程更经济。

多任务和多线程操作系统之间的区别 (Differences between multitasking and Multithreading Operating System)

  • In the multitasking system, multiple processes can be executed whereas in multithreading a single process with multiple threads can be executing at the same time.

    多任务系统中 ,可以执行多个进程,而在线程中,可以同时执行具有多个线程的单个进程。

  • Multithreading helps when more than one client uses it. For instance, In DB. While I'm typing something, there would be someone else, doing the same type of job. not more than one person will be able to do the same kind of job If DB is not having a multithread option.

    当多个客户端使用多线程时,多线程将提供帮助。 例如,在数据库中。 在输入内容时,会有其他人在做相同类型的工作。 如果DB没有多线程选项,则最多只能由一个人完成相同的工作。

  • It is time-consuming to create and manage processes than threads.

    与线程相比,创建和管理进程非常耗时。

翻译自: https://www.includehelp.com/operating-systems/multitasking-and-multithreading.aspx

java线程和操作系统线程

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值