Preventing Thread Execution: yield(), join(), and sleep() π΄
In Java, thread execution can be controlled using methods like yield()
, join()
, and sleep()
. These methods help in pausing, rescheduling, or waiting for other threads, ensuring efficient execution.
1. Why Control Thread Execution? π€
If threads execute without coordination, it can lead to inefficient resource usage, race conditions, or unexpected behavior.
Real-Life Example ππ¨
In a relay race:
- A runner may slow down (yield) to let another runner take the lead.
- A runner must wait (join) for their teammate before continuing.
- A runner can take a short break (sleep) before starting again.
2. Using yield() π
The yield()
method hints to the scheduler that the current thread is willing to pause and allow other equal-priority threads to run.
Example Code π
class YieldExample extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " is executing");
Thread.yield(); // Hints that other threads can run
}
}
public static void main(String[] args) {
YieldExample t1 = new YieldExample();
YieldExample t2 = new YieldExample();
t1.start();
t2.start();
}
}
π Note: yield()
does not guarantee that another thread will runβit just gives a chance to equal-priority threads.
3. Using join() π
The join()
method ensures that a thread waits for another thread to complete before proceeding.
Example Code π
class JoinExample extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " - " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
}
}
public static void main(String[] args) {
JoinExample t1 = new JoinExample();
JoinExample t2 = new JoinExample();
t1.start();
try {
t1.join(); // Main thread waits for t1 to finish
} catch (InterruptedException e) {}
t2.start(); // t2 starts only after t1 finishes
}
}
β Use Case: Ensuring that one thread completes before another starts.
4. Using sleep() β³
The sleep()
method pauses execution for a specified time, allowing other threads to run.
Example Code π
class SleepExample extends Thread {
public void run() {
for (int i = 1; i <= 3; i++) {
try {
Thread.sleep(1000); // Pauses execution for 1 second
} catch (InterruptedException e) {}
System.out.println(Thread.currentThread().getName() + " - " + i);
}
}
public static void main(String[] args) {
SleepExample t1 = new SleepExample();
t1.start();
}
}
πΉ Use Case: Simulating delays or pauses in execution.
5. Key Differences π
Method | Function |
yield() | Hints at scheduler to allow other threads to execute |
join() | Makes the calling thread wait until another finishes |
sleep() | Pauses execution for a specified duration |
6. Conclusion π―
- Use
yield()
to allow other threads a chance to run. - Use
join()
to ensure one thread completes before another starts. - Use
sleep()
to introduce delays in execution.
Proper thread control improves efficiency and prevents unnecessary CPU usage in Java applications! π
Subscribe to my newsletter
Read articles from ππ¬π³π¦π°π₯ ππ¬πΆππ© directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
