Skip to main content

Creating and Running a Java Thread:

Ways to create a Thread:

You can create a Thread in two ways:

  • By extending the Thread class.
  • By implementing a Runnable Interface.

Thread class:

The Thread class provides constructors and methods to create and perform operations on a thread.

Runnable Interface:

Runnable Interfaces have only one method, named run(). The Runnable Interface should be performed by classes where their instances plan to be executed by a thread. public void run(): is used to perform the action for a Thread.

Starting a thread:

The start() method of a Thread class is used to start a newly created thread. It performs the following tasks:

  • A new thread starts(with their stack).
  • The Thread moves to the Runnable state from New state.
  • The run() method will run when the Thread gets a chance.

Java Thread Example by extending Thread class:

  1. class Multi extends Thread{  
  2. public void run(){  
  3. System.out.println("Extended Thread Class Example: thread is running...");  
  4. }  
  5. public static void main(String args[]){  
  6. Multi threadOne=new Multi();  
  7. threadOne.start();  
  8.  }  
  9. }  
Output:Extended Thread Class Example: thread is running...


Java Thread Example by implementing Runnable interface:

  1. class Multi implements Runnable{  
  2. public void run(){  
  3. System.out.println("Runnable Interface Example: thread is running...");  
  4. }  
  5.   
  6. public static void main(String args[]){  
  7. Multi multiOne=new Multi();  
  8. Thread threadOne =new Thread(multiOne);  
  9. threadOne.start();  
  10.  }  
  11. }  
Output:Runnable Interface Example: thread is running...

Popular posts from this blog

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...

Atomic operation and classes in the Java Concurrency API:

Atomic Operation and Class: An operation is classified as atomic if it is performed as a single unit of work without the possibility of interference from other processes. In the Java language specification, they guarantee that the reading or writing operation is atomic (unless the variable is a long or a double). Variables of type long or double in operations are only atomic if declared with the keyword: volatile. Suppose we assume that  i  is defined as an int. The  i++  (increment) operation is not an atomic operation in Java. This also applies to the other numeric types, e.g. long.  This is because the  i++  operation first reads the value currently stored in  i , and then it adds one to it. However, between the read and the write, the value of  i  might have changed. Since Java 1.5, the java language provides an atomic class with variables such as AtomicInteger or AtomicLong. The class also provides methods like getAndDecrement(), ge...