重线和轻线 [英] Heavy weight and light weight thread

查看:149
本文介绍了重线和轻线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就Java而言,轻量级线程和重量级线程是什么?

What are the Light weight and heavy weight threads in terms of Java?

推荐答案

这与与线程关联的上下文"的数量有关,因此与执行上下文切换"所花费的时间有关.

It's related to the amount of "context" associated with a thread, and consequently the amount of time it takes to perform a "context switch".

重量级线程(通常是内核/操作系统级别的线程)具有很多上下文(硬件寄存器,内核堆栈等).因此在线程之间切换需要花费大量时间.重量级线程也可能对其有限制,例如,在某些操作系统上,内核线程不能被抢占,这意味着只有在放弃控制之前,它们才能被强行关闭.

Heavyweight threads, (usually kernel/os level threads) have a lot of context (hardware registers, kernel stacks, etc). So it takes a lot of time to switch between threads. Heavyweight threads may also have restrictions on them, for example, on some OSes, kernel threads cannot be pre-empted, which means they can't forcibly be switched out until they give up control.

另一方面,轻量级线程(通常是用户空间线程)的上下文要少得多. (它们基本上共享相同的硬件上下文),它们只需要存储用户堆栈的上下文,因此切换轻量级线程所需的时间要短得多.

Lightweight threads on the other hand (usually, user space threads) have much less context. (They essentially share the same hardware context), they only need to store the context of the user stack, hence the time taking to switch lightweight threads is much shorter.

在大多数操作系统上,与内核空间线程相比,您在用户空间中以程序员身份创建的任何线程都是轻量级的.没有重量级和轻量级的正式定义,它只是具有更多上下文的线程和具有更少上下文的线程之间的比较.别忘了每个操作系统都有自己不同的线程实现,而且不一定明确定义重线程和轻线程之间的界线.在某些编程语言和框架中,当您创建线程"时,您甚至可能没有获得完整的线程,而可能只是获得了某种抽象,它隐藏了下面的实际线程数.

On most OSes, any threads you create as a programmer in user space will be lightweight in comparison to the kernel space threads. There is no formal definition of heavyweight and lightweight, it's just more of a comparison between threads with more context and threads with less context. Don't forget that every OS has its own different implementation of threads, and the lines between heavy and light threads are not necessarily clearly defined. In some programming languages and frameworks, when you create a "Thread" you might not even be getting a full thread, you might just be getting some abstraction that hides the real number of threads underneath.

[某些操作系统允许线程共享地址空间,因此通常很重的线程会更轻一些]

[Some OSes allow threads to share address space, so threads that would usually be quite heavy, are slightly lighter]

这篇关于重线和轻线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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