Understanding the Differences Between Java's yield() and join() Methods
This article explains Java thread scheduling, priority, and the distinct behaviors of the yield() and join() methods, providing code examples that illustrate how each method influences thread execution order and synchronization in multithreaded applications.
Multithreading questions are popular in interviews, and although many developers rarely build complex multithreaded applications, understanding concepts like thread priority, yield() , and join() can boost confidence.
Java Virtual Machine uses a priority‑based scheduler: each thread receives an integer priority (1–10) that can be changed by the developer, and the OS selects the highest‑priority Java thread to run, potentially pre‑empting lower‑priority threads.
Key points about thread priority:
Threads without an explicit priority have the normal priority.
Priority values range from 1 (lowest) to 10 (highest), with 5 as normal.
The highest‑priority thread is favored for execution, but this does not guarantee it will start immediately.
Running threads may have higher priority than threads waiting in a pool.
Thread priority is set via t.setPriority() before the thread starts, using constants such as MIN_PRIORITY , MAX_PRIORITY , and NORM_PRIORITY .
yield() method
Calling Thread.yield() signals to the scheduler that the current thread is willing to give up its current use of the processor, allowing other threads of the same priority to run. It is a static native method and only moves a thread from the running state to the runnable state; it does not guarantee immediate execution of another thread.
Important characteristics:
Static native method.
Hints the scheduler to let a same‑priority thread run.
Does not guarantee rapid state change.
Only changes the thread from running to runnable, not to waiting or blocked.
yield() example
package test.core.threads;
public class YieldExample {
public static void main(String[] args) {
Thread producer = new Producer();
Thread consumer = new Consumer();
producer.setPriority(Thread.MIN_PRIORITY); // Min Priority
consumer.setPriority(Thread.MAX_PRIORITY); // Max Priority
producer.start();
consumer.start();
}
}
class Producer extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("I am Producer : Produced Item " + i);
Thread.yield();
}
}
}
class Consumer extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("I am Consumer : Consumed Item " + i);
Thread.yield();
}
}
}Without yield() , the consumer (high priority) usually prints all its messages before the producer. With yield() , the producer and consumer alternate, demonstrating cooperative scheduling.
join() method
The join() method causes the current thread to wait until the target thread completes. It can also accept a timeout, after which the waiting thread proceeds even if the target thread is still running.
Signature:
public final void join() throws InterruptedExceptionBoth sleep and join respond to interruptions by throwing InterruptedException .
join() example
package test.core.threads;
public class JoinExample {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("First task started");
System.out.println("Sleeping for 2 seconds");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("First task completed");
}
});
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("Second task completed");
}
});
t.start(); // Line 15
t.join(); // Line 16 – main thread waits for t to finish
t1.start();
}
}Output shows that the main thread waits for the first task to finish before the second task starts, illustrating how join() enforces ordering between threads.
These small but important concepts help developers control thread execution and synchronization in Java.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.