multithreading 什么是多线程应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1313062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a multithreaded application?
提问by Frank
I am looking to learn more about threading and I wanted to know: what is a multithreaded application?
我想了解更多关于线程的知识,我想知道:什么是多线程应用程序?
回答by Esteban Küber
Multithreadingas a widespread programming and execution model allows multiple threads to exist within the context of a single process. These threads share the process' resources but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. However, perhaps the most interesting application of the technology is when it is applied to a single process to enable parallel execution on a multiprocessor system.
多线程作为一种广泛使用的编程和执行模型,允许在单个进程的上下文中存在多个线程。这些线程共享进程的资源但能够独立执行。线程编程模型为开发人员提供了一个有用的并发执行抽象。然而,该技术最有趣的应用可能是将其应用于单个进程以在多处理器系统上实现并行执行。
That means that a single processcan have many different "functions" executingconcurrently, allowing the application to better use the available hardware (multiple cores/processors). Threads can communicate between them (they have shared memory), but its a hard problemto have every thread behave wellwith otherswhen accesing shared objects/memory.
这意味着单个进程可以同时执行许多不同的“功能” ,从而允许应用程序更好地使用可用硬件(多核/处理器)。线程可以在它们之间进行通信(它们具有共享内存),但是在访问共享对象/内存时让每个线程与其他线程都表现良好是一个难题。
Threading allows an application to remain responsive, without the use of a catch all application loop, when doing lengthy operations.
线程允许应用程序在执行冗长的操作时保持响应,而无需使用捕获所有应用程序循环。
For example, a non threaded copy
program wouldn't allow you to do anything until the copy completes.
例如,copy
在复制完成之前,非线程程序不允许您执行任何操作。
Threading helps with complex, lenghty, independent problems, but brings along a lot more complexity, that makes it hard even for seasoned developers.
线程有助于解决复杂的、冗长的、独立的问题,但带来了更多的复杂性,即使对于经验丰富的开发人员来说也很难。
回答by eduffy
It's an application that can do multiple things at once. For example, if you're tying a document in Word, there's a thread responding to your keyboard, there's a thread that's checking your spelling, there's one that's checking your grammar, there may be another thread saving a backup of your document in case the program crashes.
这是一个可以同时做多件事的应用程序。例如,如果您在 Word 中绑定文档,则有一个线程响应您的键盘,有一个线程在检查您的拼写,有一个线程在检查您的语法,可能还有另一个线程保存您的文档备份以防万一程序崩溃。
回答by Welbog
It's an application that uses more than one threadinternally to accomplish its goal.
它是一种在内部使用多个线程来实现其目标的应用程序。
There are lots of examples, as most application that need to interact with a user have a UI thread and a set of working threads. This is done to allow the UI to remain responsive while the application is busy doing some task.
有很多例子,因为大多数需要与用户交互的应用程序都有一个 UI 线程和一组工作线程。这样做是为了允许 UI 在应用程序忙于执行某些任务时保持响应。
回答by Mark Redman
A multi-threaded application takes advantage of running multiple tasks at the same time to speed things up. Multithreading can also take advantage of multiple CPU machines.
多线程应用程序利用同时运行多个任务来加快速度。多线程还可以利用多个 CPU 机器。
回答by ripper234
It is a program that uses more than one thread. The different threads can access shared memory structures (usually by using appropriate synchronization mechanisms, e.g. locks). An example would be a program that downloads a few files concurrently, each download using a different thread to speed up the download process (there are more sophisticated ways to achieve that, this is just an example).
它是一个使用多个线程的程序。不同的线程可以访问共享内存结构(通常通过使用适当的同步机制,例如锁)。一个例子是一个同时下载几个文件的程序,每次下载使用不同的线程来加速下载过程(有更复杂的方法来实现这一点,这只是一个例子)。
Multi-threading is often used on CPU-bound tasks, that benefit from using all cores in a modern computer (e.g. trying to break a cypher using multiple processors).
多线程通常用于受 CPU 限制的任务,这受益于使用现代计算机中的所有内核(例如尝试使用多个处理器破解密码)。
The difference between a thread and a process is that different processes usually cannot directly share memory and data structures, although various mechanisms to share information between processes exist (they are usually more costly than sharing information between threads).
线程和进程的区别在于,不同的进程通常不能直接共享内存和数据结构,尽管存在各种进程间共享信息的机制(它们通常比线程间共享信息的开销更大)。
回答by melo
what he said
他说的话
The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
线程和进程的实现因操作系统而异,但在大多数情况下,线程包含在进程中。多个线程可以存在于同一个进程中并共享内存等资源,而不同的进程不共享这些资源。
回答by ET-TAOUSY Zouhair
Multithreading is a mechanism of programing that you can implement in order to gain a remarkable time.
多线程是一种编程机制,您可以实施它以获得非凡的时间。
so a Multithreading application is an application that uses more than two threads for two processor or more and it doesn't make sense to have more threads than processor it should be the same.
所以多线程应用程序是一个应用程序,它为两个或更多处理器使用两个以上的线程,并且线程数多于处理器是没有意义的,它应该是相同的。
回答by Shashank Bodkhe
Multithreaded applications are the ones which uses concept of Concurrency i.e. they are capable of processing more than one tasks in parallel.
多线程应用程序是使用并发概念的应用程序,即它们能够并行处理多个任务。
A simple example could be a word-document in which , spell-check, response to keyboard, formatting etc happens at the same time or Concurrently. Internally there are different threads which are doing these task independently.
一个简单的例子可能是一个单词文档,其中拼写检查、键盘响应、格式设置等同时或并发发生。内部有不同的线程独立执行这些任务。
Source : https://docs.oracle.com/javase/tutorial/essential/concurrency/
来源:https: //docs.oracle.com/javase/tutorial/essential/concurrency/
回答by techboyz
for thread u have to know process which is nothing but instance of program take an example of paint in windows when u run it,it make an one instance or process of paint program. When u open mulitple image on diffenrent window u r making a multiple process of that program. Likewise thread is a unit of process mean u see a paint window but in background there are multiple threads eg.color,brush,pencil,etc. Thread is there to reduce workload of processor
对于线程,您必须知道进程只是程序实例,当您运行它时,以 Windows 中的绘制为例,它会生成一个绘制程序的实例或进程。当您在不同的窗口上打开多个图像时,您正在对该程序进行多个进程。同样,线程是一个处理单元,意味着您会看到一个油漆窗口,但在后台有多个线程,例如颜色、画笔、铅笔等。线程是为了减少处理器的工作量