![]() For example, in a word processor, while one thread is printing or saving the file, another thread can be used to continue typing. Multithreading is also necessary to provide better interactivity with the users. For example, while one thread is blocked (e.g., waiting for completion of an I/O operation), another thread can use the CPU time to perform computations, resulted in better performance and overall throughput. Multithreading within a program improves the performance of the program by optimizing the usage of system resources. The code running within the thread works only within that context, hence, a thread (of a sequential flow of operations) is also called an execution context. For example, a thread has its own stack, registers and program counter. A thread must carve out its own resources within the running process. It is lightweight because it runs within the context of a heavyweight process and takes advantage of the resources allocated for that program and the program’s environment. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process. Within a process or program, we can run multiple threads concurrently to improve the performance. It is called heavyweight because it consumes a lot of system resources. A process or program has its own address space and control blocks. Pre-emptive multitasking systems: Tasks are given time-slices of the CPU(s) and will be forced to yield control to other tasks once their allocation is used up.This has the drawback that a run-away or uncooperative task may hang the entire system. Co-operative multitasking systems: Each task must voluntarily yield control to other tasks.There are generally two kinds of multitasking operating systems: Multitasking is necessary in today's operating systems for better performance by making full use and optimize the usage of the computing resources. In a multi-CPU machine, a few tasks can be executed simultaneously, either distributed among or time-slicing the CPUs. In a single-CPU machine, only one task can be executed at one time – through time-slicing of the CPU. A multitasking system can perform many tasks concurrently by sharing the computing resources, such as CPU(s), main memory, and I/O channels. Modern operating systems (such as Windows and UNIX) are multitasking system. The following figure shows a program with 3 threads running under a single CPU: Multitasking (or Multi-processing) A thread by itself is not a program because it cannot run on its own. During the lifetime of the thread, there is only a single point of execution. A thread, also called a lightweight process, is a single sequential flow of programming operations, with a definite beginning and an end. Java has built-in support for concurrent programming by running multiple threads concurrently within a single program. The term " concurrency" refers to doing multiple tasks at the same time. A multi-thread program has an initial entry point (the main() method), followed by many entry and exit points, which are run concurrently with the main(). A single-thread program has a single entry point (the main() method) and a single exit point. Java supports single-thread as well as multi-thread operations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |