CodesJava

Easy learning with example program codes

Which is a better way to create a thread in java


As we discussed earlier that we can create a thread by following two ways.

  1. By implementing Runnable interface.
  2. By extending Thread class.

Which is a better way to create a thread in java

  • By implementing Runnable interface is a better way to create a thread in java because when we create a thread by extending Thread class, all Thread class methods are inherited while we can perform the task with the one method (run method) only. It results into overhead inheritance.
  • Other reason is that java does not support multiple inheritance in case of classes. So if we create a thread by extending the Thread class, we will not be able to extend any other class.

1. Create thread example by implementing Runnable interface:

To create a thread using Runnable interface, create a class which implements Runnable interface. Runnable interface have only one method run().

Syntax: public void run().

run() method will contain the code for created thread.

Now create a thread class object explicitly because our class is not extending thread class and hence its object can’t be treated as thread object. Pass class object that implements Runnable interface into thread class constructor to execute run method.

Example:

CreateThreadExample1.java

/**
 * This program is used to create thread using Runnable interface.
 * @author codesjava
 */
class Test implements Runnable{
	public void run(){
		System.out.println("thread started.");
	}
}
 
public class CreateThreadExample2 {
	public static void main(String args[]){
		//creating Test object.
		Test obj = new Test();
 
		//creating thread
		Thread thrd = new Thread(obj);
 
		//start the thread.
		thrd.start();
	}
}

Output:

thread started.

Download this example.

2. Create thread example by extending Thread class:

Thread class provides methods to perform operations on threads.

Thread class is in Java.lang package.

Syntax: public class Thread extends Object implements Runnable.

 Commonly used constructors of Thread class:

1. Thread().
2. Thread(Runnable target).
target – the class object whose run method is invoked when this thread is started. If null, this thread’s run method is invoked.
3. Thread(String name).
name – the name of the new thread.
4. Thread(Runnable target, String name).
target – the class object whose run method is invoked when this thread is started. If null, this thread’s run method is invoked.
name – the name of the new thread.

Example:

/**
 * This program is used to create thread using Thread class.
 * @author codesjava
 */
class Test extends Thread{
	public void run(){
		System.out.println("thread started.");
	}
}
 
public class CreateThreadExample1 {
	public static void main(String args[]){
		//creating thread.
		Test thrd1 = new Test();
 
		//start the thread.
		thrd1.start();
	}
}

Output:

thread started.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki