A thread in Java is the smallest unit of execution within a process. Threads allow multiple tasks to execute concurrently while sharing the resources of the same process. Threads are commonly used for tasks such as background processing, file operations, network requests, and handling multiple client requests.
Threads share the memory and resources of their process.
Java provides the Thread class and Runnable interface for creating threads.
Create Threads
There are two traditional ways to create a thread in Java:
Thread-Creation
1. By Extending Thread Class
Create a class that extends Thread. Override the run() method, this is where you put the code that the thread should execute. Then create an object of your class and call the start() method. This will internally call run() in a new thread.
Java
importjava.io.*;importjava.util.*;classMyThreadextendsThread{// initiated run method for Threadpublicvoidrun(){Stringstr="Thread Started Running...";System.out.println(str);}}publicclassGeeks{publicstaticvoidmain(Stringargs[]){MyThreadt1=newMyThread();t1.start();}}
Output
Thread Started Running...
Explanation:
MyThread extends the Thread class.
The run() method contains the task performed by the thread.
t1 creates a thread object.
start() starts a new thread.
The JVM then invokes the run() method on that new thread.
2. Using Runnable Interface
Create a class that implements Runnable. Override the run() method, this contains the code for the thread. Then create a Thread object, pass your Runnable object to it and call start().
Java
importjava.io.*;importjava.util.*;classMyThreadimplementsRunnable{// Method to start Threadpublicvoidrun(){Stringstr="Thread is Running Successfully";System.out.println(str);}}publicclassGeeks{publicstaticvoidmain(String[]args){MyThreadg1=newMyThread();// initializing Thread ObjectThreadt1=newThread(g1);// Running Threadt1.start();}}
Output
Thread is Running Successfully
Explanation:
MyTask implements Runnable.
The run() method defines the task.
A MyTask object is created.
The object is passed to the Thread constructor.
start() creates a new thread and executes run().
Note: Extend Thread when when you don’t need to extend any other class. Implement Runnable when your class already extends another class (preferred in most cases).
Thread vs Runnable
Feature
Extending Thread
Implementing Runnable
Approach
Extend Thread
Implement Runnable
Task and thread
Combined
Separated
Can extend another class?
No
Yes
Reusability
Less flexible
More flexible
Recommended
For simple specialized thread behavior
Preferred for defining a task
Life Cycle of a Thread
During its thread life cycle, a Java thread transitions through several states from creation to termination.
New State
Runnable State
Blocked State
Waiting State
Timed Waiting State
Terminated State
Thread Life Cycle
Running Threads
There are two methods used for running Threads in Java:
run() Method: Contains the code for the thread. Calling it directly behaves like a normal method call.
start() Method: Launches a new thread and internally calls run() concurrently.
Example: Using Thread Class and Runnable Interface
Java
// Thread class implementationclassThreadImplextendsThread{@Overridepublicvoidrun(){// Output: Thread Class RunningSystem.out.println("Thread Class Running");}}// Runnable interface implementationclassRunnableThreadimplementsRunnable{@Overridepublicvoidrun(){// Output: Runnable Thread RunningSystem.out.println("Runnable Thread Running");}}publicclassGeeks{publicstaticvoidmain(String[]args){// Create and start Thread class threadThreadImplt1=newThreadImpl();t1.start();// Create and start Runnable interface threadRunnableThreadr=newRunnableThread();Threadt2=newThread(r);t2.start();// Wait for both threads to completetry{t1.join();// Wait for t1t2.join();// Wait for t2}catch(InterruptedExceptione){e.printStackTrace();}}}
Output
Thread Class Running
Runnable Thread Running
Note: We use start() to launch a new thread, which then calls the run() method in parallel. If we call run() directly, it works like a normal method call and no new thread is created.
Java Thread Class
The Thread class is used to create and control threads in Java. Each object of this class represents a single thread of execution.
Syntax
public class Thread extends Object implements Runnable
Advantages of Threads
Improved performance: Multiple threads can execute tasks concurrently.
Better resource utilization: Threads share the same memory and resources.
Responsive applications: UI applications remain responsive while performing background tasks.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.