Creating and Starting Threads - 3 ways

1) Thread Subclass
2) Runnable Interface Implemention
3) Callable (call) and Futures (get) - BEST and Latest (with help of Executors(submit (callable obj))

Method 1
public class MyThread extends Thread {
    
    public void run(){
       System.out.println("MyThread running"); 
    }
  }
 
MyThread myThread = new MyThread();
myTread.start(); 

OR

You can also create an anonymous subclass of Thread like this:

  Thread thread = new Thread(){
    public void run(){
      System.out.println("Thread Running");  
    }
  }
   thread.start();

Method 2
public class MyRunnable implements Runnable {

    public void run(){
       System.out.println("MyRunnable running");
    }
  }
   Thread thread = new Thread(new MyRunnable());
   thread.start(); 
 
OR
 
You can also create an anonymous implementation of Runnable, like this:
   Runnable myRunnable = new Runnable(){

     public void run(){
        System.out.println("Runnable running");
     }
   }
   Thread thread = new Thread(myRunnable);
   thread.start();
 
Method 3: 
 Using Callable and Futures - refer next post
 
 

Subclass or Runnable?

There are no rules about which of the two methods that is the best. Both methods works. Personally though, I prefer implementing Runnable, and handing an instance of the implementation to a Thread instance. When having the Runnable's executed by a thread pool it is easy to queue up the Runnable instances until a thread from the pool is idle. This is a little harder to do with Thread subclasses.
Sometimes you may have to implement Runnable as well as subclass Thread. For instance, if creating a subclass of Thread that can execute more than one Runnable. This is typically the case when implementing a thread pool.

Common Pitfall: Calling run() instead of start()

When creating and starting a thread a common mistake is to call the run() method of the Thread instead of start(), like this:
Thread newThread = new Thread(MyRunnable());
thread.run(); //should be start();
At first you may not notice anything because the Runnable's run() method is executed like you expected. However, it is NOT executed by the new thread you just created. Instead the run() method is executed by the thread that created the thread. In other words, the thread that executed the above two lines of code. To have the run() method of the MyRunnable instance called by the new created thread, newThread, you MUST call the newThread.start() method.
 

0 comments: