Threads - BASIC CONCEPTS
Most of us are confused between multitasking and
multithreading. In this post let us try to give a brief description about
multitasking, Threads and multithreading. Multithreading is one of the major
features of Java.
What is multitasking?
Before learning about multitasking we should understand “what
is a process?” A process is a task which is under execution. So multiple
processes running at the same time is known as multitasking.
Example:
When you open a web browser and a word document at
the same time, you are making the operating system to undergo multitasking. So
multitasking is a method of running more than one task simultaneously. We say
that both the processes are executing simultaneously but in reality the
instructions are executed one at a time. Then how do we justify our statement “multitasking“?
Here comes the concept of context switching. The control of execution jumps
between the processes with negligible time gaps. The time gaps are so small
that both the processes appear to be running simultaneously.
Why multitasking?
Suppose a process is being executed by the processor. After sometime an interrupt
occurs ( for example an I/O interrupt ) . Until and unless the interrupt is
served the processor has to be idle. But this is not the efficient way as we
are not using the resources of the processor efficiently. During the interrupt
if we allow the processor to process other requests there will be effective
utilization of the processor instead of making it to sit idle.
Now let us know something about multithreading.
Before getting into the definition of multithreading we must
understand what do we mean by a thread. “A thread is a part of a process “. In
multithreading we can divide the process into multiple threads and execute them
simultaneously. In this way we can speed up the process of execution of a
particular process.
So how do we create a thread? Whenever the JVM calls the
main method it creates a thread automatically known as “main thread”. Now if we
would like to create threads in our process then we have to opt for two
methods. One way is to use “Thread”
class available in java.lang.Thread
package. The second method is to implement Runnable interface. Let us discuss
both these methods in detail.
So multitasking is running multiple processes simultaneously
whereas multithreading is dividing the same process into multiple sub-processes
with all the sub-processes executing simultaneously.
Everything in java is object. So whenever an object is
created, the reference of the created
object is given to the object . For example consider:
MyClass class=new
MyClass();
Whenever this statement is written the expression on the
right hand side of the assignment operator is evaluated which creates an object
for the MyClass class and returns the reference to the class object.
In the similar manner we create an object for the thread
class as:
Thread t=new Thread();
The class which is used to create thread must extend the
Thread class.
Class MyClass extends Thread{
}
Here all the properties and methods of the Thread class can
now be accessed by the child class MyClass. Main method is also a thread. The
body of the main thread is written in the main(). Inorder to create our known
thread , the body of our thread must be written in a special method called
run() which is available in the Thread class.
Class MyClass extends Thread{
Public void run(){
}
}
We can compile the MyClass class even if we do not have a
main() inside the class
but cannot execute it.
Let us write a program which displays numbers from 1 to 5 by
the child thread( user created thread) and main thread displaying 6 to 10.
Class Uthread extends Thread{
Public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println(i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
Here we have used the try{} catch{} block because the
Thread.sleep() might throw an InterruptedException which is a checked
exception.
Why Thread.sleep(long) ?
Whenever the loop is executed, the value is printed and then
the thread is slept for 1000ms =1 sec.
During this time the control returns to
the parent thread and continues executing other instructions of the main thread.
After the sleep time is over the control gets back to the MyClass class and the
execution continues from where it left off.
The second method to use thread is by implementing Runnable
interface. Now we may have a question as “ When thread class exists then what
is the use of runnable interface ?”
The answer is that Java does not support multiple
inheritance. For example if I have a class A which extends class B. Now if I
need to extend Thread class it is not possible since a class cannot extend more
than one class in java. In order to solve this problem Runnable interface has
been introduced.
Example for runnable interface :
Class Rthread implements Runnable {
Public void run(){
System.out.println(“Thread with runnable”);
}
Public static void main(String args[]) {
Rthread thread=new Rthread();
Thread t1=new Thread(thread);
t1.start();
}
}
THREAD PRIORITIES:
Each thread will have priority number in the range of 1-10
where the priority of main method is 5.
“Why do we require a priority?”
When more than one threads are executing simultaneously, the
processor might not know which thread to execute first. In order to solve this
problem priorities have been introduced where each and every thread is provided
with some priority at the time of creation. The thread with higher priority
will have the higher probability of execution. The minimum value of priority
for a thread is zero and the maximum priority is 10. The average priority of a
thread is 5.
As we all know that whenever main() is called by the JVM a
thread is created which is called main thread. When a thread is created in the
main function it is assigned a priority of the main thread which is 5. So the
thread with higher priority will execute more number of times than the thread
with lower priority.
getPriority() is the method which is used to get the priority of a particular thread.
Comments
Post a Comment
Thanks for your comments!