• August 29, 2014
  • Core Java
  • Comments Off on Way of creating thread in java

Way of creating thread in java

Way of creating thread:

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

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 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 javawithease
 */
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 javawithease
 */
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:

Multithreading in java.
Thread life cycle in java.
Way of creating thread in java.
Commonly used methods of Thread class.
Thread scheduling in java with example.
Thread priority in java with example.
Naming a thread in java with example.
Joining a thread in java with example.
Daemon thread in java with example.
Can we start a thread twice?
Can we call run method directly?
Difference between Thread.yield() and Thread.sleep() methods.
Deadlock in java with example.
Starvation in java with example.
Inter-thread communication in java with example.
Synchronization in java with example.
Synchronized method in java with example.
Static synchronization in java with example.
Synchronized block in java with example.

 


Comments are closed.