Thread Programming
Thread Programming
Thread Programming
THREAD PROGRAMMING
Introduction
A Thread sometimes called a lightweight process (LWP), is a basic unit of CPU utilization, it
comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads
belonging to the same process its code section, data section and other system resources. A traditional
(or heavyweight) process has a single thread of control. If a process has multiple threads of control, it
can do more than one task at a time.
Java is one of a small number of languages that provide support at the language level for the creation
and management of threads. All Java programs comprise at least a single thread of control. Even a
simple Java program consisting of only a “main” method runs a single thread in the JVM. In
addition, Java provides commands that allow the developer to create and manipulate additional
threads of control within the program. Classes and interfaces for creating threads are part of core
java.lang package and the class java.lang.Object, from which all Java objects descend, has methods
to support multithreaded programming. To take advantage of the powerful networking features of
Java, one has to use threads and multithreaded programming in the network programs appropriately.
Address Space
Most modern operating systems can run multiple processes at the same time such as we might run
our email client, editor and compiler all at once. Modern desktop operating system enforce a
separation between the memory areas used by these programs, so a bug in the compiler can’t crash
the editor and the email client can’t break the compiler. Each of these separate memory areas is
called an address space.
Context Switching
The operating system is responsible for scheduling the processes ad managing the address space for
each one. Switching between processes is called context switching and requires the operating
system to save the whole state of the running process. If the computer has more than one processor,
the operating system could even schedule them to run simultaneously. Because the processes are
completely independent, the system as a whole is reliable even if the individual programs are not.
Threads
A process contains one or more independent units of execution called threads. Threads are similar to
processes in that they are independent flows of control, but they are also different in important ways–
The threads within a process run within a single address space, so the threads operate using
the same body of code and the same body of data.
Because the threads are operating within a single process, the Java Virtual Machine rather
than operating system can do the thread scheduling. This can make context switching much
more efficient.
Example:
//Program displaying single Thread program
//ThreadTest.java
import java.lang.*;
class ThreadTest
{
public static void main(String[] args)
{
Thread t = Thread.currentThread();
System.out.println(“Mainthread” + t);
t.setName(“Namechange”);
System.out.println(“ChangedThread” +t);
Dead