Multi-Threading in Java

Published: Last Updated on 1.1K views 3 minutes read
A+A-
Reset

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.

Thread share address space:

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:

Every java program have at least one thread. That is main thread which is created by JVM at the start of program execution.

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.

Multi-Threading Examples;

Have a look at following program:

package intro;
public class ChildThread1 extends Thread 
{
 public void run()
 {
    for(int i=0;i<10;i++)
    {
      System.out.println("ChildThread1 "+i);
    }
 }
}
package intro;
class MainClassThread 
{
    public static void main(String[] args)throws InterruptedException
    {
      ChildThread1 t1=new ChildThread1();
      t1.start();
      for(int i=0;i<10;i++)
      {
        System.out.println("Main thread "+i);
      }
    }
}
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.
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.

 
Output:
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

We will go in more detail through Multi-threading on other posts.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.