Home »
Java programming language
How to Create Java Threads (java examples to create threads)?
In this article, we will learn how we can create threads in java? Here, we have examples, in which we are creating threads by extending Thread class and by implementing Runnable interface.
Submitted by Jyoti Singh, on December 04, 2017
Threads are lightweight processes. A process is a complete program while a thread is a small task which may or may not independent. Each java program contains a main thread which is responsible for the execution of main method.Threads is basically used for asynchronous tasks that is for background processing and to use the concept of multitasking.
In java threads can be created in two ways:
- By extending Thread class
- By implementing Runnable interface
1) Program to create thread by extending Thread class
In this program, a class named newThread extends Thread class which is an inbuilt class of java has functions like run(), stop(), start(), destroy() etc. The start method will be invoked on an object of newThread class. This thread will keep running to 10000 times as per logic.
class newThread extends Thread{
private int threadid;
//this is a constructor to take the arguments in a thread
public newThread(int id)
{
//here threadid will be 1
threadid = id;
}
public void run() {
/*run() method is responsible for running a thread ,all the programming logic will
be contain by this thread i.e what u want your thread to do */
for(int i=0; i<10000 ; i++) {
System.out.println(threadid + " : " + i);
}
}
}
public class CreateThreadByExtending {
public static void main(String[] args) {
//creating an object of newThread class and sending 1 as an argument
newThread thread1=new newThread(1);
//start() function is use to start the thread
thread1.start();
}
}
Output
1 : 0
2 : 1
3 : 2
4 : 3
upto 9999
2) Program to create thread by implementing Runnable interface
In this program, a class named newThread1 implements a Runnable interface which is an inbuilt interface of java. Here an object will be created of class newThread1 and this object will be passed to Thread class to invoke the run method this is because objects of class newThread1 will not be considered as Thread object.
class newThread1 implements Runnable{
public void run() {
/*run() method is responsible for running a thread ,all the programming logic will
be contain by this thread i.e what u want your thread to do */
System.out.println("Thread is running.........");
}
}
public class CreateThreadByImplementing {
public static void main(String[] args) {
//creating an object of newThread class
newThread1 m1=new newThread1();
/*as class newThread class doesn't extend Thread class ,therefor its object will not be consider as
thread object.We need to explicitily call Thread class object and passing the object of newThread class
that implements runnable so run() method will be executed.*/
Thread t1 =new Thread(m1);
//starting the thread
t1.start();
}
}
Output
Thread is running.......
TOP Interview Coding Problems/Challenges