← Back to Book Detail

13 Multithreading — Java Programming (13/7) -- Java Programming

Browse
185%

13 Multithreading — Java Programming

13 Multithreading — Java Programming 13.1 Multithreading Basics In concurrent programming, there are two basic units of execution: processes and threads. The ability to have more than one program working at the same time is called multitasking. The main objective of multi tasking is to minimise execution time for faster delivery of result and maximize CPU Utilisation by running several tasks at the same time. It can be done in two ways, depending on whether the operating system interrupts programs without consulting with them first, or whether programs are only interrupted when they are willing to yield control. The former is called pre-emptive multitasking. The latter is called cooperative multitasking or non-preemptive multitasking. Multi tasking can be done using two methods: i) multi processing, ii) multithreading 13.1.1 Multiprocessing Multi tasking using multi processing requires multiple processors. A simple java program normally requires one processor. If a user simultaneously wants to run another process, the necessary actions are taken up to submit it to another processor in the cores. 13.1.2 Multithreading Multithreading programs extend the idea of multitasking by taking it one level lower. Individual programs will appear to do multiple tasks at the same time. Each task is called a thread and programs that can run more than one thread at once are said to be multithreaded. It is very useful in practice. For example, the Java virtual machine itself uses a thread for garbage collection. The GUI programs have a separate thread for gathering user interface events from the host-operating environment. 13.1.3 Multiprocessing vs Multithreading Multitasking threads require minimum overheads than multitasking processes. Processes are heavy tasks that require their own separate address spaces. Inter process communication is expensive and limited. Context switching from one process to other is also costly. Threads, on the other hand, are light components. They share the same address space and cooperatively share the same heavy process. Inter thread communication is inexpensive, and context switching from one thread to the next is of low cost. 13.2 The Java Thread Model In an operating system context, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. In many cases, a thread is a component of a process. [13.1] A thread is thus a smallest portion of the process. A thread is an independent, virtual and sequential control flow within a process. Each thread performs the job independently of another thread. The multiple threads of a given process may be executed concurrently (via multithreading capabilities), sharing resources such as memory, while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its dynamically allocated variables and non-thread-lo
← Previous Chapter Next Chapter →