CodesJava

Easy learning with example program codes

Way of creating thread in java


Way of creating thread:

  1. By implementing .
  2. By extending .

Important points:

  • We discussed earlier that every thread has a job associated with it. This job is what we are performing in run method.
  • By default every program have one main thread. It may have number of daemon threads. GC is a daemon thread. It is the responsibility of main thread to start the child threads.

1. 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 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. 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.

Download this example.
 
Next Topic: Commonly used methods of Thread class.
Previous Topic: Thread life cycle in java.

Related Topics:

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


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap