Multithreading · Lesson 101 of 150
Thread Methods
Thread methods are built-in methods provided by the Thread class to control thread behaviour — like start() to begin a thread, sleep() to pause it temporarily, and join() to wait for another thread to finish.
01What are Thread Methods?
Thread methods are built-in methods provided by the Thread class to control thread behaviour — like start() to begin a thread, sleep() to pause it temporarily, and join() to wait for another thread to finish.
02Why is it used?
These methods give you control over how and when threads run, pause, or wait for each other, which is essential for coordinating multiple threads working together correctly.
03Real-Life Example
Think of a relay race where one runner must wait (join()) for the previous runner to finish before starting their own leg. These thread methods manage this kind of coordination between multiple independent tasks.
04Syntax
javathread.start(); Thread.sleep(milliseconds); thread.join(); thread.setPriority(value);
05Example Program
javapublic class ThreadMethodsDemo { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> System.out.println("Task done")); t.start(); t.join(); // main thread waits for t to finish System.out.println("Main thread continues"); } }
Output:
Task done
Main thread continues06Key Points to Remember
sleep()pauses the current thread for a given time, without releasing any locks it holds.join()makes the calling thread wait until the target thread finishes execution.- Thread priority (
setPriority()) is only a hint to the scheduler — it doesn't guarantee a strict execution order.