Home >> Java >> Multi-Threading in Java
MultiThreading in java
MultiThreading in java

Multi-Threading in Java

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.

  1.    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.
  2. 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()
  • 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:

Multi-Threading in java
Multi-Threading in java

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
ChildThread1 0
Main thread 1
ChildThread1 1
Main thread 2
ChildThread1 2
Main thread 3
ChildThread1 3
Main thread 4
Main thread 5
Main thread 6
Main thread 7
Main thread 8
ChildThread1 4
Main thread 9
ChildThread1 5
ChildThread1 6
ChildThread1 7
ChildThread1 8
ChildThread1 9

About Log Raj Bhatt

Check Also


Interface with method body

Up to now we never said about using method body inside an Interface. But since …

Leave a Reply

Your email address will not be published. Required fields are marked *