Thread
is the pre-defined class in Java which is used to create and manage threads (independent paths of execution).
Thread
class is present in the java.lang
package.
package java.lang;
public class Thread extends Object implements Runnable
{
// Fields
// Constructors
// Methods
}
Thread
Class:
Thread
class with examples:
Constructor | Description |
---|---|
Thread() |
Creates a new thread with no target and a generated name. |
Thread(Runnable target) |
Creates a new thread that will execute the specified Runnable object. |
Thread(Runnable target, String name) |
Creates a new thread with a target task and a custom name. |
Thread(String name) |
Creates a new thread with a custom name but no target. |
Thread(ThreadGroup group, Runnable target) |
Creates a new thread inside a specified thread group with a target task. |
Thread(ThreadGroup group, Runnable target, String name) |
Creates a new thread inside a group with a target task and custom name. |
Thread(ThreadGroup group, Runnable target, String name, long stackSize) |
Creates a new thread with group, target, name, and custom stack size. |
Thread
class:
// Demonstration of Thread class constructors
class MyRunnable implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread().getName() + " is running...");
}
}
public class ThreadConstructorsDemo
{
public static void main(String[] args)
{
// 1. Default constructor
Thread t1 = new Thread();
System.out.println("Thread t1 (default): " + t1.getName());
// 2. Thread(Runnable target)
Thread t2 = new Thread(new MyRunnable());
t2.setName("RunnableThread");
t2.start();
// 3. Thread(Runnable target, String name)
Thread t3 = new Thread(new MyRunnable(), "Worker-1");
t3.start();
// 4. Thread(String name)
Thread t4 = new Thread("HelperThread");
System.out.println("Thread t4 (only name): " + t4.getName());
// 5. Thread(ThreadGroup group, Runnable target)
ThreadGroup g1 = new ThreadGroup("GroupA");
Thread t5 = new Thread(g1, new MyRunnable());
t5.setName("GroupThread-1");
t5.start();
// 6. Thread(ThreadGroup group, Runnable target, String name)
ThreadGroup g2 = new ThreadGroup("GroupB");
Thread t6 = new Thread(g2, new MyRunnable(), "GroupThread-2");
t6.start();
// 7. Thread(ThreadGroup group, Runnable target, String name, long stackSize)
ThreadGroup g3 = new ThreadGroup("GroupC");
Thread t7 = new Thread(g3, new MyRunnable(), "GroupThread-3", 1024);
t7.start();
}
}
Thread t1 (default): Thread-0 RunnableThread is running... Worker-1 is running... Thread t4 (only name): HelperThread GroupThread-1 is running... GroupThread-2 is running... GroupThread-3 is running...
Thread
Class:
Thread
class:
Method | Description / Use | More |
---|---|---|
start() |
Starts the execution of the thread (internally calls run() ). |
|
|
Contains the code that will be executed by the thread. | |
|
Returns a reference to the currently executing thread object. | |
|
Checks whether the thread is still running. | |
|
Returns the name of the thread. | |
|
Sets or changes the name of the thread. | |
|
Checks whether the thread is a daemon thread. | |
|
Marks the thread as a daemon thread or a user thread. | |
|
Returns the priority of the thread (1 to 10). | |
|
Changes the priority of the thread (1 = MIN, 5 = NORM, 10 = MAX). | |
|
Makes the currently executing thread pause for the specified time. | |
|
Pauses the current thread and allows other threads of equal priority to execute. | |
|
Makes one thread wait for another thread to finish execution. | |
|
Interrupts a thread if it is waiting, sleeping, or blocked. | |
| ||
interrupted() |
Checks whether the current thread has been interrupted and clears the interrupt flag. |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.