Java Threads Benefits

  1. Threads are light weight when compared to process
  2. Threads takes less time to create than process
  3. Threads consume less resources than process
  4. Threads share parent process code and data
  5. Threads can easily create new threads than process creating a new process
  6. Communication between threads is easy than inter process communication

Creating Thread

Java Virtual Machine allows application to have multiple threads. These threads can run concurrently. Each thread has a priority and based of priority threads will be executed. Higher the priority more preference will be given to thread.

When a thread creates another thread, then initial priority of a new thread will be equal to the priority of the creating thread.

If daemon thread creating a new thread then new thread will be a daemon.

Ways to Create Threads

There are two ways to create threads. One by extending a Thread class and overriding the run method of a Thread class. Second is by implementing a Runnable interface.

  1. Extending a Thread class (java.lang.Thread)
  2. Implementing a Runnable interface (java.lang.Runnable)

Thread Class

We can extend java.lang.Thread class to create our own java thread class. We have to override run() method. Now we can create object of our class and call start() method to execute run() method of our custom java thread class.

public class MyThread extends Thread
{

    public void run()
    {
        System.out.println("Hello from Thread !!!");
    }

    public static void main(String args[])
    {
        (new MyThread()).start();
    }

}

The output of above code will be

Hello from Thread !!!

Above output printed by a thread which got created from the main method by calling start() method of MyThread class.

In above example MyThread is the same class which has main() method.

Another example for creating a thread by creating a separate class. Here we will be extending a Thread class. And we will create a thread from the main() method from other class.

public class MyThread extends Thread {

    public MyThread(String name) {
        //naming Thread by calling a constructor of Thread class
        super(name);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " Started");
        try {
            Thread.sleep(2000);
            //Do some other processing
            doProcessing();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " End");
    }

    private void doProcessing() throws InterruptedException {
        System.out.println("Processing ...");
        //processing
        Thread.sleep(3000);
    }

}

public class Main {

    public static void main(String[] args)
    {
        MyThread t1 = new MyThread("Thread1");
        t1.start();
    }
}

Output of the above program will be

Thread1 Started
Processing ...
Thread1 End

References


https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

LEAVE A REPLY

Please enter your comment!
Please enter your name here