Reputation: 157
I have following classes:
package net.adjava.multithreading;
public class MyResource {
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
package net.adjava.multithreading;
public class Thread1 extends Thread {
MyResource m;
public Thread1(MyResource m) {
super();
this.m = m;
}
@Override
public void run() {
System.out.println("Current Thread name1 :"
+ Thread.currentThread().getName());
for (int i = 0; i < 10000; i++) {
m.setA(i);
System.out.println("Set method sets the value of a as: " + i);
System.out.println("Current Thread name1 :"
+ Thread.currentThread().getName());
Thread.yield();
}
}
}
package net.adjava.multithreading;
public class Thread2 extends Thread {
MyResource m;
public Thread2(MyResource m) {
super();
this.m = m;
}
@Override
public void run() {
System.out.println("Current Thread name2 :" + Thread.currentThread().getName());
for (int i = 0; i < 100; i++) {
System.out.println(" value of a as per getter method is :"
+ m.getA());
System.out.println("Current Thread name2 :" + Thread.currentThread().getName());
}
System.out.println("waiting for thread to end");
}
}
package net.adjava.multithreading;
public class ThreadExecutionPoint {
public static void main(String args[])
{
System.out.println("Current Thread name main :" + Thread.currentThread().getName());
MyResource m = new MyResource();
Thread1 th1 = new Thread1(m);
Thread2 th2 = new Thread2(m);
th1.start();
th2.start();
}
}
I am trying to understand the purpose of yield() with the above classes. Before trying this example all i knew about yield() was that it pauses the thread which calls it. So to test it i used the yield method in Thread1 class. So basically as per my understanding thread1 should execute for loop for one time and then should have paused and waited for other threads to complete. but the output show a different result. The output shows that first thread1 gets executed and then thread2. Can someone please correct what i am missing ? or is something wrong with my understanding for yield().
Upvotes: 3
Views: 1825
Reputation: 70929
I quote from here:
This static method is essentially used to notify the system that the current thread is willing to "give up the CPU" for a while. The general idea is that: The thread scheduler will select a different thread to run instead of the current one. However, the details of how yielding is implemented by the thread scheduler differ from platform to platform. In general, you shouldn't rely on it behaving in a particular way. Things that differ include: when, after yielding, the thread will get an opportunity to run again; whether or not the thread foregoes its remaining quantum.
So in short you never know when the thread execution will again be started.
Upvotes: 3
Reputation: 7226
The documentation states this related to yield
method:
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Even thought that might happen, nothing can guarantee you that the thread that will be chosen to be processed, isn't the same one.
As almost everything concertning threads, do not rely on this, as the functionality you expected is not guaranteed.
Upvotes: 4