THREAD CLASS
What is java threads?
• Java provides Thread class to achieve thread
programming. Thread class
• provides constructors and methods to create and
perform operations on a thread. Thread class
Basic thread methods
Modifier and Type Method Description
void start() It is used to start the
execution of the thread.
void run() It is used to perform action
for a thread.
Static void sleep() It sleeps a thread for the
specified amount of time.
Static thread currentThread() It returns a reference to the
currently executing thread
object.
void join() It waits for a thread to die.
Int getPriority() It returns the priority of the
thread.
void setPriority() It changes the priority of the
thread.
Modifier and Type Method Description
String getName() It returns the name of the
thread.
void setName() It changes the name of
the thread.
Long getId() It returns the id of the
thread.
Boolean isAlive() It tests if the thread is
alive.
Static void yield() It causes the currently
executing thread object to
temporarily pause and
allow other threads to
execute.
Void suspend() It is used to suspend the
thread.
Void resume() It is used to resume the
suspended thread.
Void stop() It is used to stop the
thread.
Void destroy() It is used to destroy the
• Threads are lightweight subprocesses, representing the smallest unit of
execution with separate paths. The main advantage of multiple threads
is efficiency (allowing multiple things at the same time). For example,
in MS Word, one thread automatically formats the document while
another thread is taking user input. Additionally, multithreading
ensures quick response, as other threads can continue execution even
if one gets stuck, keeping the application responsive.
Life cycle of threads
• There are different states Thread transfers into during its
lifetime, let us know about those states in the following
lines: in its lifetime, a thread undergoes the following
states, namely:
• New State
• Active State
• Waiting/Blocked State
• Timed Waiting State
• Terminated State
Life cycle of threads
Java main thread
• As we are familiar, we create Main Method in each and
every Java Program, which acts as an entry point for the
code to get executed by JVM, Similarly in this
Multithreading Concept, Each Program has one Main
Thread which was provided by default by JVM, hence
whenever a program is being created in java, JVM
provides the Main Thread for its Execution.
How to Create Threads in Java?
• Extending Thread Class
• Implementing a Runnable interface
1. By Extending Thread Class
We can run Threads in Java by using Thread Class,
which provides constructors and methods for creating
and performing operations on a Thread, which extends a
Thread class that can implement Runnable Interface. We
use the following constructors for creating the Thread:
Example
import java.io.*;
import java.util.*; Output:
Thread Started Running...
class MyThread extends Thread {
// initiated run method for Thread
public void run() {
String str = "Thread Started Running...";
System.out.println(str); }}
public class Geeks{
public static void main(String args[]) {
MyThread t1 = new MyThread();
t1.start(); }}
Using Runnable Interface
import java.io.*;
import java.util.*;
class MyThread implements Runnable {
// Method to start Thread
public void run() {
String str = "Thread is Running Successfully";
System.out.println(str); }}
public class Geeks{
public static void main(String[] args) {
MyThread g1 = new MyThread();
// initializing Thread Object Output:
Thread t1 = new Thread(g1); Thread is Running Successfully
// Running Thread
t1.start(); }}
Running Threads in Java
There are two methods used for running Threads in Java:
• run() Method in Java
• start() Method in Java