Java – Thread vs Runnable

What is Thread ?

thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. (Java – Thread vs Runnable is a method of execution). Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. In default, thread priority will be defined as 5(between 0-10). Each thread may or may not also be marked as a daemon. When code running in some thread creates a new objectThread, the new thread has its priority initially set equal to the priority of the creating thread and is a daemon thread if and only if the creating thread is a daemon.

Java - Thread vs Runnable
Thread vs Runnable

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

what is Runnable Interface ?

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.Java – Thread vs Runnable

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Threadmethods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Java – Thread vs Runnable :


/**
 *
 * @author Vivek Java
 */
class ThreadRunn1  implements Runnable {

    @Override
    public void run() {       
        //System.out.println("Implements Method  - END "+EXTThread.currentThread().getName());
        System.out.println("Implements Method  - END "+Thread.currentThread().getName());
    }
}
class ThreadRunn  implements Runnable {

    @Override
    public void run() {       
        //System.out.println("Implements Method  - END "+EXTThread.currentThread().getName());
        System.out.println("Implements Method  - END "+Thread.currentThread().getName());
    }
}

class EXTThread extends Thread{
    EXTThread(){}
    @Override
    public void run() {       
        System.out.println("Extends Method - END "+Thread.currentThread().getName());
    }
}
class ThreadSample {
    public static void main(String args[]){
        System.out.println("Initial "+Thread.currentThread().getName());
        ThreadRunn run = new ThreadRunn();
        run.run();
        ThreadRunn1 run1 = new ThreadRunn1();
        run1.run();
        EXTThread th = new EXTThread();
        th.start();
        Thread th1 = new Thread(run);
        th1.start();
    }
}

Output :

Initial main
Implements Method – END main
Implements Method – END main
Implements Method – END Thread-1
Extends Method – END Thread-0

Explanation :

  • run – In this object, I am creating a class by implementing the Runnable interface.
    • Based on the output the run object was not creating a new thread is running on the main daemon thread.
  • run1 – – In this object, I am using runnable interface so because of that it is running on main thread.
  • th- object was created by inheriting Thread class so based on the output you can see, This was running in new Thread (
    Thread-1 ).
  • th1 – In this object, I am creating a Thread object by passing a runnable interface class as an argument, so this runnable class will run in the separate Thread ( Thread-0 ).

When we should use Runnable :

  • whenever not your feeling that you can’t control the thread, it’s good to use runnable interface.
  • Because runnable interface will be running on the main thread/existing thread so the creation of thread will be controlled by the JVM.
  • To increase performance we can use runnable for simple task/process.
  • If you’re using runnable you will not get all the features of a thread.

When we should use Thread:

  • If you have the requirement to run multiple tasks concurrently.
  • There are a few times when there’s absolutely no point in using multiple threads. For instance, if your application is bound by a single resource (e.g. the disk, or the CPU) and all the tasks you would use multiple threads for will all be trying to use that same resource, you’ll just be adding contention. Java – Thread vs Runnable, for instance, suppose you had an application which collected all the names of files on your disk. Splitting that job into multiple threads isn’t likely to help – if anything, it’ll make it worse because it would be asking the file system for lots of different directories all at the same time, which could make the head seek all over the place instead of progressing steadily.
  • Similarly, if you have a workflow where each stage relies entirely on the results of the previous stage, you can’t use threads effectively. For instance, if you have a program which loads an image, rotates it, then scales it, then turns it into black and white, then saves it again, each stage really needs the previous one to be finished before it can do anything.
  • Suppose, however, you wanted to read a bunch of files and then process their contents (e.g. calculating the cryptographic hash using an algorithm which takes a lot of processor power) then that might very well benefit from threading – either by having several threads doing both or one thread dedicated to disk IO and another dedicated to hash calculation. Java – Thread vs Runnable the latter would probably be better, but would probably involve more work as the threads would need to be passing data to each other rather than just doing their own thing. Even though here there is still a dependency between the data being read from disk and the crypto processing, you don’t have to read all the data from the disk before you can start processing it.

Useful Programs:

Related Searches:

  • Java – Thread vs Runnable
  • Java Runnable
  • Threads in Java
  • Java thread example
  • Multithreading in java



Sample Project




You may also like...