开发者

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

开发者 https://www.devze.com 2023-01-27 04:00 出处:网络
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 s

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(){
   开发者_如何学C     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(); } }


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消