0
public class YieldDemo extends Thread{

   public static void main(String[] args) {
        YieldDemo y1 = new YieldDemo();
        YieldDemo y2= new YieldDemo();

        y1.start();
        y2.start();
   }

   public void run() {
      for(int i=0;i<=5;i++) {
           if(i==3) {
               Thread.yield();
           } else
               System.out.println(i+Thread.currentThread().toString());
           }
   }
}

As per the documentation of yield(), thread-1 should yield and allow thread-2 to process after 3rd loop. However, the output is not as expected. Same thread continues skipping 3rd iteration. After one thread completes the loop, other thread executes with same behaviour. Please explain.

Output:

0Thread[Thread-1,5,main] 
1Thread[Thread-1,5,main] 
2Thread[Thread-1,5,main] 
4Thread[Thread-1,5,main] 
5Thread[Thread-1,5,main] 
0Thread[Thread-0,5,main] 
1Thread[Thread-0,5,main] 
2Thread[Thread-0,5,main] 
4Thread[Thread-0,5,main] 
5Thread[Thread-0,5,main] 
René Link
  • 48,224
  • 13
  • 108
  • 140
NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • Output: 0Thread[Thread-1,5,main] 1Thread[Thread-1,5,main] 2Thread[Thread-1,5,main] 4Thread[Thread-1,5,main] 5Thread[Thread-1,5,main] 0Thread[Thread-0,5,main] 1Thread[Thread-0,5,main] 2Thread[Thread-0,5,main] 4Thread[Thread-0,5,main] 5Thread[Thread-0,5,main] – NaveenBharadwaj Aug 22 '14 at 05:35
  • if you increasing the loop iterations ? it has the same behavior ? – USer22999299 Aug 22 '14 at 05:38
  • Yes. I tried with 50,100. Everytime, only the iteration for which if condition satisfies is skipped and same thread continues until the end of the loop.Then the next thread starts its execution. – NaveenBharadwaj Aug 22 '14 at 05:42

3 Answers3

5

The java.lang.Thread.yield() method causes the currently executing thread object to temporarily pause and allow other threads to execute.

NOTE : That other thread can be same thread again. There is no guarantee which thread be chosen by JVM.

codingenious
  • 8,385
  • 12
  • 60
  • 90
  • if that is the case, if I increase the loop counter to say 100, the threads run for longer time. According to the statement you made, we cannot guarantee which thread be chosen. So, the output must be unpredictable. But in this case, output remains same every time. How is that possible? – NaveenBharadwaj Aug 22 '14 at 05:44
  • Also note that it is possible for the first thread to be finished before the second thread is started. Btw on my machine the program works as you are expecting, changing thread at the yield statement. – Gustav Grusell Aug 22 '14 at 05:45
  • @GustavGrusell, two threads start simultaneously and no part of code is synchronized. Then if for loop executes for longer time (say i<100), both threads can execute simultaneously, right? Correct me if I'm wrong. – NaveenBharadwaj Aug 22 '14 at 05:49
  • They may execute simultaneously, but they are not guaranteed to do so. My point is since they are started not at the same time, but one after another, it is possible that the first one finishes before the second on starts. – Gustav Grusell Aug 22 '14 at 06:29
2

As with almost all aspects of Multithreading, even your case isn't guaranteed to behave as expected. Thread.yield() is just like a suggestion to the OS telling - if it is possible, then please execute other threads before this one. Depending on the architecture of your system (number of cores, and other aspects like affinity etc etc) the OS might just ignore your request.

Also, after JDK6U23, the JVM might just change your code to :

   public void run() {
      for(int i=0;i<=5;i++) {
   // 3 is too darn small. and yield() is not necessary
   // so let me just iterate 6 times now to improve performance.  
     System.out.println(i+Thread.currentThread().toString());

   }

yield() can totally be ignored (which might be happening in your case. If you are getting the same result over and over again)

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • As far as I can see, the code you posted does not produce the same result as the original code, since it produces output also for i==3. – Gustav Grusell Aug 22 '14 at 15:00
  • @GustavGrusell - I said with multi threading nothing can be predicted. because yield() is not like wait() / notify(); which acquire / release lock on Object's monitor. – TheLostMind Aug 22 '14 at 17:29
  • I think I agree with what you are trying to say, and I also agree with your original post. I was just trying to point out that you've made a small mistake in your code example. The JVM will not change the code in such a way that extra lines of output are produced. – Gustav Grusell Aug 22 '14 at 19:00
  • @GustavGrusell - ya.. maybe. Now, when I look carefully, the condition itself seems flawed. if(i is 3) then yield else print i makes very little sense. – TheLostMind Aug 23 '14 at 05:57
0

Read This article. yield method is to request for a thread to sleep. it may be happen or not.

Community
  • 1
  • 1
user3717646
  • 436
  • 3
  • 10