Home‎ > ‎Applied Core Java‎ > ‎

Threads

Computer does more than one things at a time for example while you are working on word processor your other application is downloading multiple files, media player is playing song, editor is printing pages, and clock is displaying current time.

Even multiple tabs of single browser can download multiple websites at the same time. Such kind of application execution is called concurrency.
Computer has ability to execute multiple programs (tasks or processes) at the same time using time slicing preemptive system. This is referred as multitasking or concurrency.

Time Slice is the period of time allowed for a process to run in the preemptive scheduling system. After each time slice the scheduler searches for next process to run. This process is called time slicing.

The act of taking control of the CPU by operating system from one task and giving it to another task is called preempting.
After multi-tasking it came multithreading that means multiple threads can be executed concurrently within a process.

In concurrent programming, there are two basic units of execution: processes and threads. A computer system normally has many active processes and threads.

Figure 1 : Process and Threads

A computer system normally has many active processes and threads.

Processes
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

Processes can not share  its memory and other resources. Two processes can communicate with each others with help of environment variables.

Threads
Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.  Since threads can share their allocated resources (memory, open files, open connections etc.) that is why it is called lightweight processes.

Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files.

Multithreaded execution is an essential feature of the Java platform. Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling. But from the application programmer's point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads, as we'll demonstrate in the next section.

<<Previous | Next>>
Comments