Programs normally reside on disks. Once they are activated, they are called processes. In today’s multitasked operating systems, each process is given a slice of the CPU time which is approximately 5-20 milliseconds. Now, in single threaded processes, when a process blocks for I/O such as for getting input, we can easily see that it wastes CPU time. So for efficiency purpose, programs are made multithreaded. A multithreaded program contains two or more parts of a program that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate part of the execution. If one thread of a process blocks for I/O, another thread of the process performing a different task may run. Multithreading approach is mostly used in applications that are interactive like word processors. In word processors, one thread is receiving input, another thread may be auto-saving it and another thread may be performing grammar checks, etc. Any application we are working on that requires two or more things to be done at the same time is a best one for use of threads.
Java allows programs to be multithreaded. 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 halts 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 I/O. At any time, a thread can be terminated, which halts its execution immediately. Once terminated, a thread cannot be resumed.
Java assigns a priority value to each thread that determines how that thread should be treated with respect to other threads. Thread priorities are integers that specify the relative priority of one thread to another. A thread’s priority is used to decide when to switch from one running thread to the next. It is called context switch. A context switch occurs in two ways:
· A thread can voluntarily relinquish control: This is done by explicitly yielding, sleeping or blocking on pending I/O. In this scenario, all other threads are examined, and the highest priority thread that is ready to run is given the CPU.
· A thread can be preempted by a higher priority thread: In this case, a lower priority thread that does not yield the processor is preempted by a higher priority thread. It is also called preemptive multitasking.
In Windows, threads of equal priority are time-sliced in a round-robin fashion.
To create a new thread in Java, we need to either extend the Thread class or to implement the Runnable interface. Some of the methods defined in the Thread class are:
Extending the Thread Class
A can be made runnable as a thread by extending the class java.lang.Thread. This provides access to all the thread methods directly. It includes the following steps:
1) Declare the class as extending the Thread class.
class MyThread extends Thread
{
… … … … … … … … …
}
2) Implement the run() method that is responsible for executing the sequence of code that the thread will execute.
public void run()
{
… … … … … …
}
3) Create a thread object and call the start() method to initiate the thread execution.
MyThread aThread = new MyThread();
aThread.start(); //invokes run() methods
Example:
class A extends Thread
{
public void run()
{
for(int i=1;i<=1000;i++)
{
System.out.println("\t From ThreadA:i="+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=1000;j++)
{
System.out.println("\t From ThreadB:j="+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=1000;k++)
{
System.out.println("\t From ThreadC:k="+k);
}
System.out.println("Exit from C");
}
}
public class ThreadTest {
public static void main(String []a)
{
new A().start();
new B().start();
new C().start();
}
}
Implementing the Runnable interface
The Runnable interface declares the run() method that is required for implementing threads in the programs. To do this, the following steps are to be followed:
1. Declare the class as implementing Runnable interface.
2. Implement the run() method.
3. Create an object of the class implementing the Runnable interface.
4. Create another thread object of the Thread class and pass the object created in step 3 to call the constructor of Thread class.
5. Call the thread’s start() method to run the thread.
Example:
class X implements Runnable
{
public void run()
{
for(int i=1;i<=1000;i++)
{
System.out.println("\tThreadX:"+i);
}
System.out.println("End of ThreadX");
}
}
public class RunnableTest {
public static void main(String []a)
{
X runnable = new X();
Thread threadX = new Thread(runnable);
threadX.start();
System.out.println("End of main Thread");
}
}
No comments:
Post a Comment