Victor
Victor

Reputation: 21

Java yield() method does not work. Netbeans Ubuntu 10.04

I'm working with Threads in Java using Netbeans 6.9.1 on Ubuntu 10.04 x86_64. I have a problem using the method yield() because when I invoke this method the current thread keeps running instead of stopping and let other threads execution.

The code below it's an easy example to run 2 threads using yield. Instead of run the first thread, print one line and then stop the thread, the program finishes the thread 1 and then runs thread2, as the method yield is not called. I have tested this code on Windows and it works perfectly! so I wonder if there is any issue to use this method on Ubuntu or on 64bits platforms.

Any idea? Thanks in advance.


//ThreadTest.java
public class ThreadTest extends Thread{
    public ThreadTest (String name){
        super(name);
    }
    public void run(){
        for (int i=0;i<5;i++){
            System.out.println(getName()+" - "+i);
            yield();
        }
        System.out.println(" END "+getName());
    }
}

//Main.java public class Main { public static void main(String[] args) { ThreadTest t1 =new ThreadTest("Thread1"); ThreadTest t2 =new ThreadTest("Thread2"); t1.start(); t2.start(); } }

Upvotes: 2

Views: 1054

Answers (2)

codeplay
codeplay

Reputation: 610

The javadoc for yield() method in sun JDK 6 and JDK 7 is different, you may need to check the javadoc for the version of JVM you are using.

Upvotes: 1

casablanca
casablanca

Reputation: 70711

yield is simply a request for another thread to be scheduled. There is nothing that prevents the JVM or underlying OS from scheduling the same thread again.

Upvotes: 12

Related Questions