Before Meshing in the Multi-Threading we should have some idea about multi-tasking. Multi_tasking is approach in which we can execute multiple tasks simultaneously. I’ve separated examples of multitasking in two types.
- Process based Multitasking: In this type each task is independent program (process) and we can execute several processes simultaneously. For example listening music while coding and checking emails also. These are operating system level multitasking examples.
- Thread based Multitasking: This is kind of multi-tasking where each task is separate independent part of same program. For example the grammar and spell checking while typing or auto correction and hints. As being programmer we’ll concern on thread based multitasking which are realized at program level.
You have heard that context switch is easier in thread than processes because threads share same address space. That’s right and mean that threads have their separate stack, thread data, registers etc BUT they share heap memory and any change they have made is accessible to each other. Threads run on virtual memory space of parent process. So sometimes there is case of mutual exclusion and deadlock like problems.
Application: Mainly multi-threading is used to develop multimedia graphics, animation, video games, web servers and application servers.
In java it is quit easier to develop the multi-threaded applications as compared to traditional languages.
How to create threads:
There is a class called Thread which we use to create threads. Thread class is in java.lang package so we need not to import any package.
There are in general two constructor of thread.
- Thread(Runnable r)
First we look at how to create thread extending thread and then we’ll discuss about using Runnable Interface to create thread.
Have a look at following program:
public class ChildThread1 extends Thread
public void run()
public static void main(String args)throws InterruptedException
ChildThread1 t1=new ChildThread1();
System.out.println("Main thread "+i);
lets take a glance at first piece of program. A class ChildThread1 extends Thread. We should override run method which exist in Thread class. And have to put all code in that run method which thread need to execute. Now from main method we have created an object of the class ChildThread1 which is actually thread object. Whenever we call start on that thread object new thread will started will run independently and in parallel with main. JVM creates Main thread.
And here main thread t1 is created in main thread so main is parent thread of t1 and t1 is child thread of main thread. Actually t1 is not name of thread.it is name of thread object. Thread name is assigned automatically by JVM and that may be JVM specific. Here execution of both threads is managed by thread scheduler. The output we get is mixed consist of output from both threads. Output may vary for each run.
Main thread 0
Main thread 1
Main thread 2
Main thread 3
Main thread 4
Main thread 5
Main thread 6
Main thread 7
Main thread 8
Main thread 9