What Are Threads?

翻译 2011年10月19日 10:25:42

What Are Threads?


Threads are a relatively lightweight way to implementmultiple paths of execution inside of an application.


At the system level, programs run side by side, with thesystem doling out execution time to each program based on its needs and theneeds of other programs.


Inside each program, however, exists one or more threads ofexecution, which can be used to perform different tasks simultaneously or in anearly simultaneous manner.


The system itself actually manages these threads ofexecution, scheduling them to run on the available cores and preemptivelyinterrupting them as needed to allow other threads to run.


From a technical standpoint, a thread is a combination ofthe kernel-level and application-level data structures needed to manage theexecution of code.

从技术角度来看,一根儿线程 是 两个层级(内核层级和应用程序层级)的数据结构组合,用于管理代码的执行。

The kernel-level structures coordinate the dispatching ofevents to the thread and the preemptive scheduling of the thread on one of theavailable cores.

内核层级的数据结构负责 在一个可用核心 协调 事件的派遣 和 线程抢占式调度。

The application-level structures include the call stack forstoring function calls and the structures the application needs to manage andmanipulate the thread’s attributes and state.

应用程序层级的数据结构 包括用于存储函数调用的调用堆栈和应用程序用于管理和操作的线程的属性和状态所需的结构。

In a nonconcurrent application, there is only one thread ofexecution.


That thread starts and ends with your application’s mainroutine and branches one-by-one to different methods or functions to implementthe application’s overall behavior.


By contrast, an application that supports concurrency startswith one thread and adds more as needed to create additional execution paths.


Each new path has its own custom start routine that runsindependently of the code in the application’s main routine.


Having multiple threads in an application provides two veryimportant potential advantages:


Multiple threads can improve an application’s perceivedresponsiveness.


Multiple threads can improve an application’s real-timeperformance on multicore systems.


If your application has only one thread, that one threadmust do everything.


It must respond to events, update your application’swindows, and perform all of the computations needed to implement yourapplication’s behavior.


The problem with having just one thread is that it can onlydo one thing at a time.


So what happens when one of your computations takes a longtime to finish?


While your code is busy computing the values it needs, yourapplication stops responding to user events and updating its windows.


If this behavior continues long enough, a user might thinkyour application is hung and try to forcibly quit it.


If you moved your custom computations onto a separatethread, however, your application’s main thread would be free to respond touser interactions in a more timely manner.


With multicore computers common these days, threads providea way to increase performance in some types of applications.


Threads that perform different tasks can do sosimultaneously on different processor cores, making it possible for anapplication to increase the amount of work it does in a given amount of time.

多根儿线程 利用 不同的cpu 内核 可以 同时 执行不同的任务,使如下诉求成为可能:

Of course, threads are not a panacea for fixing anapplication’s performance problems.


Along with the benefits offered by threads come thepotential problems.


Having multiple paths of execution in an application can adda considerable amount of complexity to your code.


Each thread has to coordinate its actions with other threadsto prevent it from corrupting the application’s state information.

每根儿线程与其他线程 相互协调行为,以避免破坏应用程序的状态信息。

Because threads in a single application share the samememory space, they have access to all of the same data structures.


If two threads try to manipulate the same data structure atthe same time, one thread might overwrite another’s changes in a way thatcorrupts the resulting data structure.


Even with proper protections in place, you still have towatch out for compiler optimizations that introduce subtle (and not so subtle)bugs into your code.


What are threads?

What are threads? Threads are lightweight processes that exist within a larger process. Threads shar...
  • fengkewei
  • fengkewei
  • 2008年05月30日 15:27
  • 504

What are threads (user/kernel)?

What are threads (user/kernel)?Threads are “light weight processes” (LWPs). The idea is a process ...
  • yin262
  • yin262
  • 2017年01月13日 11:08
  • 143

What is the difference between LWP and threads?

This explains the difference between LWP-Process-Thread:  A light-weight process (LWP) is a mea...
  • B321MW
  • B321MW
  • 2013年01月22日 16:39
  • 534

what is the difference between processes and threads

this is the classic interview question,well, in addition ,to begin with it ,let us discuss what is d...
  • u011570042
  • u011570042
  • 2013年08月26日 21:17
  • 546

Threads 的基本使用方法

POSIX Threads 1.1      Thread基本要素 ž   线程操作包括创、终止、同步(joins,blocking)、调度、数据管理、过程交互。 ž   一个线程不维持一个已创建线...
  • xiaoding133
  • xiaoding133
  • 2013年11月27日 20:32
  • 1662

What is the difference between user level threads and kernel level threads?

Answer A kernel thread, sometimes called a LWP (Lightweight Process) is created and scheduled by the...
  • jicheng687
  • jicheng687
  • 2009年09月07日 14:54
  • 712

android 读取 json 字符串文件遇到的一个坑 W/art: Suspending all threads took: xx.xxx ms

需求背景: json文件比较大 有各种换行 简单的读写 public static String getStringFromRaw(Context context,int rawID...
  • RichieZhu
  • RichieZhu
  • 2016年09月30日 14:48
  • 6124

Perl threads 摘要

最近又写了一个多线程的小工具,对一些多线程的使用有了进一步的心得。 Perl 创建线程有两种方式,正常通过threads->create 创建线程,用async  创建一个调用匿名过程的线程...
  • USTBHacker
  • USTBHacker
  • 2012年12月27日 16:37
  • 3337

Socket连接错误之toString() unavailable - no suspended threads

nnd,真想骂街。。。 最近公司新搞了一个项目,我负责在原来的app的基础上,改一下新的ip和端口,对新项目进行测试。本来是一个好好地app,跟服务器的socket连接啥的也都是嗖嗖的,结果,今天下...
  • u010483897
  • u010483897
  • 2017年12月22日 17:46
  • 270

What are hogging threads? When do threads become hogged? After what period of time?

http://easyloadrunner.blogspot.com/2013/10/what-are-hogging-threads-when-do.html   According to th...
  • jamex
  • jamex
  • 2014年12月18日 13:56
  • 644
您举报文章:What Are Threads?