Can You Kill a Thread Without Killing the Process?
Computer programs run in memory, the processor can handle different application processes. Within these application processes, programmers create smaller sub-programs called "threads" that run concurrently with each other in the program. Threads are not processes, and are not responsible for the main processes that created them. Stopping the execution of a thread that does not represent the main process of an application will not stop the process itself.
-
Concurrency
-
Developers use the term "concurrency" to denote the capability of a program or programming language to run multiple threads of execution at the same time. When computers gained the ability to run multiple programs, these programs could all reside in memory, running concurrently. The actual support of concurrency in a programming language means that it can support concurrent threads in the main programmatic process.
Processes
-
The execution of a program follows certain steps. The processor first loads the program code from the hard drive into main memory. The operating system designates a specific address space in memory for the program, so that information from the program does not leak into other programs. The program begins execution, then becomes a running process. Programs usually have a main running process, but may consist of several smaller processes.
-
Threads
-
Processes are considered heavy-weight, in that the operating system assigns them dedicated memory space, and the processes must communicate through special variables to prevent race conditions or memory lock. Threads are lightweight processes that reside inside a single process. A process may have numerous threads, but threads only reside in one process. Threads can interact with each other, share information, start and stop with less system involvement or resources.
Deleting Threads
-
Threads usually are not deleted. They finish execution and then rejoin the main thread. This is referred to as "thread death." The main process can wait for running threads to die, either through some variable value change or by enacting a particular function. Threads that are created in a process that stop execution will not typically halt execution of the program. The thread will exit with an error that the process can handle. The main process, which can also be considered the main thread, can die. The program will then halt.
-