How to Implement Thread in Java ?

Google+ Pinterest LinkedIn Tumblr +

How to implement Thread in JAVA
In my opinion Thread is the most wonderful feature of JAVA and I remember when I started learning JAVA in one of programming class in India how important Thread was portrait and how much emphasis given on clear understanding of multi threading. It’s indeed still popular and one of most sought after skill in JAVA.
In this core java tutorial I will share my experience on different way of implementing Thread in Java; this is also a very common core java interview question and asked mostly during junior level java interview.

There are two ways of implementing threading in JAVA
1) By extending java.lang.Thread class, or
2) By implementing java.lang.Runnable interface.

Before we go into implementation details I just like to cover when we use thread? so we use thread if we want some part of code is executed parallel and we put that code inside run() method of either Thread class or Runnable interface.

Actually public void run () method is defined in Runnable interface and since java.lang.Thread class implements Runnable interface it gets this method automatically. I remember by first java multi threading example which was an animation program where multiple threads were used in Applet to create animation of words falling from top left, middle and top right of the page. That was pretty exciting at that time because till then I only know program which takes input from command prompt and print output on command prompt.

So now the interview question “Which way of implementing Thread is better? Extending Thread class or implementing Runnable method?
In my opinion implementing Runnable is better because in Java we can only extend one class so if we extend Thread class we can not extend
any other class while by implementing Runnable interface we still have that option open with us.

Second reason which make sense to me is more on OOPS concept according to OOPS if we extend a class we provide some new feature or functionality , So if the purpose is just to use the run() method to define code its better to use Runnable interface.

till then we have just created a thread , Thread will not start until you call the start() method of java.lang.Thread class. When we call start () method Java Virtual machine execute run () method of that Thread class into separate Thread other than calling thread. Anybody guess what will happen if we call the run() method directly instead of calling start() method ?

That another interesting interview question and answer is simple there would be any Error or Exception run() method will simply be executed in the same Thread and new Thread will not be created. Another follow up question would be what will happen if you call start () method twice in same Thread object e.g.

  mythread.start();
  mythread.start();// this line will throw IllegalThreadStateException

//implementing Thread by extending Thread class
 class MyThread extends Thread{

             public void run(){
                         System.out.println(“I am executing by Thread: ” + Thread.currentThread().getName());
             }
 }

 //implementing Thread by implementing Runnable interface
 class MyRunnable implements Runnable{

             public void run(){
                         System.out.println(“I am executing by Thread: ” + Thread.currentThread().getName());
             }
 }

//starting Thread
             Thread mythread = new MyThread();
         mythread.setName(“T1”);
         Thread myrunnable = new Thread(new MyRunnable(),”T2″);

         mythread.start();
         myrunnable.start();

TIP1: It’s not guaranteed that mythread will start before myrunnable it depends upon Thread scheduler.
TIP2: Thread will be said to go on dead state once execution of run () method finished and you can not start that thread again.

you may find my other core java interview questions interesting

How get() method of HashMap works in JAVA

How classpath works in Java

What is the problem while using “==” in autoboxing world in Java 5 ?

Share.

About Author

Leave A Reply