Java Threads: How to Create and Manage Threads
Learn about the two primary ways to create a thread in Java: by extending the Thread
class and by implementing the Runnable
interface. This guide covers the essential features of the Thread
class, which provides constructors and methods for thread operations. Discover how to effectively manage threads to enhance the performance and responsiveness of your Java applications.
Java Threads | How to Create a Thread in Java
There are two ways to create a thread:
- By extending Thread class
- By implementing Runnable interface
Thread Class
Thread class provides constructors and methods to create and perform operations on a thread. It extends Object class and implements Runnable interface.
Commonly Used Constructors
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r, String name)
Commonly Used Methods
public void run()
: performs action for a thread.public void start()
: starts the execution of the thread.public void sleep(long milliseconds)
: makes the thread sleep for specified time.public void join()
: waits for a thread to die.public void join(long milliseconds)
: waits for a thread to die for specified time.public int getPriority()
: returns the thread priority.public void setPriority(int priority)
: changes the thread priority.public String getName()
: returns the thread name.public void setName(String name)
: changes the thread name.public Thread currentThread()
: returns the reference of currently executing thread.public long getId()
: returns the thread ID.public Thread.State getState()
: returns the thread state.public boolean isAlive()
: tests if the thread is alive.public void yield()
: pauses the currently executing thread.public boolean isDaemon()
: tests if the thread is a daemon thread.public void setDaemon(boolean b)
: marks the thread as daemon.public void interrupt()
: interrupts the thread.public boolean isInterrupted()
: tests if the thread has been interrupted.public static boolean interrupted()
: tests if the current thread has been interrupted.
Runnable Interface
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. It has only one method:
public void run()
: performs action for a thread.
Starting a Thread
The start()
method of Thread class starts a new thread, moving it from New state to Runnable
state. When it gets a chance to execute, its run()
method will run.
1) Java Thread Example by Extending Thread Class
Syntax
class Multi extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
Multi t1 = new Multi();
t1.start();
}
}
Output
Thread is running...
2) Java Thread Example by Implementing Runnable Interface
Syntax
class Multi3 implements Runnable {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
Multi3 m1 = new Multi3();
Thread t1 = new Thread(m1);
t1.start();
}
}
Output
Thread is running...
3) Using the Thread Class: Thread(String Name)
Syntax
public class MyThread1 {
public static void main(String[] args) {
Thread t = new Thread("My first thread");
t.start();
String str = t.getName();
System.out.println(str);
}
}
Output
My first thread
4) Using the Thread Class: Thread(Runnable r, String name)
Syntax
public class MyThread2 implements Runnable {
public void run() {
System.out.println("Now the thread is running ...");
}
public static void main(String[] args) {
Runnable r1 = new MyThread2();
Thread th1 = new Thread(r1, "My new thread");
th1.start();
String str = th1.getName();
System.out.println(str);
}
}
Output
My new thread
Now the thread is running ...