Constructors and Methods
The Thread class is used to create and manage threads. it is a part of the java.lang package and it provides methods for starting, pausing, resuming, and stopping threads.
Thread Class Constructors
The thread class provides multiple overloaded constructors that allow creation of threads with different configurations.
1. No-Argument Constructor
The Thread class was created with the default values of thread properties.Thread t1 = new Thread();
2. String-Argument Constructor
Thread t1 = new Thread("Java");
Creates a Thread class instance where the thread name will be java. Thread Id and thread priority will get default initialization.3. Runnable-argument type constructor
Let's say ‘r1’ be the object of the Runnable Interface.Runnable r1 = new MyRunnable();
Thread t1 = new Thread(r1);
4. Two-argument constructor
Runnable r1 = new MyRunnable();
Thread t1 = new Thread(r1, "Quipo");
- Creates the thread class instance with the specified runnable type and specified Thread name.
- The first argument is the Runnable type and the second argument is the String type.
Thread Class Methods
Thread class provides various methods as follows:
1. public int getId();
- By invoking this method it receives the ID of Thread.
2. public String getName();
- By invoking this method it receives the name of a Thread.
3. public int getPriority();
- This method returns the priority number of a Thread.
4. public void setName(String name);
- This method is used to set the name of a Thread.
5. public void setPriority(int priorityNum);
- This method is used to set the priority of a Thread.
6. public void start();
- On invoking this method it starts execution of a Thread.
- the start() internally calls the run method of a thread object.
7. public void run();
- The thread execution begins by calling run().
8. public void stop();
- The method is used to stop the running thread, on invoking this method the running Thread will be terminated.
9. public static void sleep(long time);
- On invoking this method it pauses the currently running thread for a specified period of time once the time elapsed the thread resumes the execution.
1. Get and Set Thread Properties
Example: Setting and Getting Thread Properties
class MyThread extends Thread {
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
System.out.println("Thread ID: " + Thread.currentThread().getId());
System.out.println("Thread Priority: " + Thread.currentThread().getPriority());
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.setName("CustomThread");
t1.setPriority(8);
t1.start();
}
}
Output:
Thread Name: CustomThread
Thread ID: 12
Thread Priority: 8
2. Thread Execution Methods
Example: Difference Between Start() and run()
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.run(); // Runs in the main thread (No new thread is created)
t1.start(); // Creates a new thread and runs in parallel
}
}
Output:
Thread is running... // (run() executed in the main thread) Thread is running... // (start() created a new thread)
3. Pause and Resuming a Thread
Example: Using sleep()
package quipoin;
public class Sample extends Thread{
public void run(){
System.out.println("Printing even numbers from 1-10");
for(int i=1;i<=10;i++){
if(i%2==0){
System.out.print(i+" ");
}
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println("Main method started");
Sample sample=new Sample();
sample.start();
}
}
Output:
Main method started
2 4 6 8 10
- On invoking start() on a thread, the thread is registered to the thread scheduler of JVM once JVM allocates a new stack it calls the run() method to do execution on the new stack.
- If we directly invoke the run() instead of start() the execution happens on the current stack which will not allow achieving parallel execution.
Key Point
- Thread constructors allow to create threads with custom names, IDs, and Runnable tasks.
- start() should always used instead of run() to enable multi-threading.
- sleep() pauses execution but not stop the thread.
- Threads have unique IDs, names, and priority values that can be change.