Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Multi Threading

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Multi Threading

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Multithreaded Programming

Java provides built-in support for multithreaded programming. A multithreaded program contains
two or more parts that can run concurrently. Each part of such a program is called a thread, and each
thread defines a separate path of execution. Thus, multithreading is a specialized form of
multitasking.
There are two distinct types of multitasking: process-based and thread-based.
A process is a program that is executing. Thus, process-based multitasking is the feature that allows
your computer to run two or more programs concurrently. For example, process-based multitasking
enables you to run the Java compiler at the same time that you are using a text editor. In process-
based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code.
This means that a single program can perform two or more tasks simultaneously. For instance, a text
editor can format text at the same time that it is printing, as long as these two actions are being
performed by two separate threads.
Multitasking threads require less overhead than multitasking processes. Processes are heavyweight
tasks that require their own separate address spaces. Inter-process communication is expensive and
limited. Context switching from one process to another is also costly. Threads, on the other hand, are
lightweight. They share the same address space and cooperatively share the same heavyweight
process. Interthread communication is inexpensive, and context switching from one thread to the
next is low cost.
Multithreading enables you to write very efficient programs that make maximum use of the CPU,
because idle time can be kept to a minimum.

The Java Thread Model:


Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency
by preventing the waste of CPU cycles.
Single-threaded systems use an approach called an event loop with polling. In this model, a single
thread of control runs in an infinite loop, polling a single event queue to decide what to do next.
Once this polling mechanism returns with, say, a signal that a network file is ready to be read, then
the event loop dispatches control to the appropriate event handler. Until this event handler returns,
nothing else can happen in the system. This wastes CPU time. It can also result in one part of a
program dominating the system and preventing any other events from being processed. In general, in
a singled-threaded environment, when a thread blocks (that is, suspends execution) because it is
waiting for some resource, the entire program stops running.
The benefit of Java’s multithreading is that the main loop/polling mechanism is eliminated. One
thread can pause without stopping other parts of your program. For ex: the idle time created when a
thread reads data from a network or waits for user input can be utilized elsewhere. When a thread
blocks in a Java program, only the single thread that is blocked pauses. All other threads continue to
run.
Threads exist in several states. A thread can be running. It can be ready to run as soon as it gets
CPU time. A running thread can be suspended, which temporarily suspends its activity. A suspended
thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when
waiting for a resource. At any time, a thread can be terminated, which halts its execution
immediately. Once terminated, a thread cannot be resumed.

You might also like