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 πŸ†

MethodFunction
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! πŸš€

0
Subscribe to my newsletter

Read articles from 𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”© directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”©
𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”©