Java Thread example

Thread can be called as light weight process. Java supports multithreading , so it allows your application to perform two or more task concurrently.  Multithreading can be of advantage specially when now a days, machine has multiple CPUs, so multiple tasks can be executed concurrently.
Whenever we call main method in java, it actually creates a single main thread. Although it creates other threads too but those are related to system and known as daemon threads. So if we want to create more threads to execute task concurrently , we can use multiThreading.

Thread can be created in two ways.

  • By extending Thread class
  • By implementing Runnable interface

By extending Thread class:

You can create your own thread by extending Thread class and override run method. You need to create object of that class and then call start() method on it to execute thread as different threads.

Create a class FirstThread.java as below.

In above program, we have created our own thread by extending Thread class and overriding run method.

Create main class named ThreadExampleMain.java

In above program ,we are creating a object of FirstThread class and calling start method to execute the thread.
When you run above program, you will get following output.
Thread is running

By implementing Runnable  interface:

The other way is , you need to implement Runnable interface and override public void run() method. You need to instantiate the class, pass created object to Thread constructor and call start method on thread object to execute thread as different threads.
Create a class FirthThread.java as below.

In above program, we have created our own class and implemented Runnable interface and overridden run() method.
Create main class named ThreadExampleMain.java

In above program ,we are creating a object of FirstThread class and passing it to Thread constructor and calling start method to actually execute.The start method will eventually call run method of FirstThread class.
When you run above program, you will get following output.
Thread is running

Thread vs Runnable which is better?

Implementing Runnable interface is considered to be better approach than Extending Thread due to following reasons.
  • Java does not support multiple inheritance so if you extend Thread class and you can not extend any other class which is needed in most of the cases.
  • Runnable interface represents a task and this can be executed with help of Thread class or Executors.
  • When you use inheritance, it is because you want to extend some properties of parent, modify or improve class behavior. But if you are extending thread class just to create thread, so it may not be recommended behavior for Object Oriented Programming.

import_contacts

You may also like:

Related Posts

  • 30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • 19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • 11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. Using Thread.sleep Sleep method causes current thread to pause for […]

  • 29 May

    How to print even and odd numbers using threads in java

    In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order […]

  • 22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • 16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.