On 9 Feb 2001, Juergen Kreileder wrote:

> BTW, it's "Thread.yield()".  "Thread.currentThread().yield()" has
> exactly the same meaning but as Thread.yield() is a class method you
> should call it directly on the class.
> "Thread.currentThread().yield()" makes it look like yield() would be
> an instance method.

Yes, but all operations refer to the current thread, and conceptually 
refer to the same object. Since java is positioned as a concurrent object 
oriented programming language, the class instance (actor) behaves as a 
forwarder for the current thread object (actor), and semantically it is 
perfectly legal to bypass the forwarding step and be explicit.

Btw, it might be interesting in an smp machine to have the capability to 
instruct other threads to yield. The current spec 
makes a_thread.yield() meaningless (<any_thread>.yield() == 
Thread.currentThread().yield()). This could be useful in user-space 
scheduler implementations and a mixed threading model.

As far as the correctness of yield behavior is concerned you are right 
(it does sth the jls correcness criteria and does not violate strong 
fairness conditions), but there is definetely sth instructive in the way the 
test_yield program behaves. If there is no I/O thread involved, then all 
threads get approximately equal run occurences (see test_fairness, attached).
If you run test_yield with a longer sleep period, you'll notice that 
there is a dominant thread which gets approx as many runs as the other 
two. Although this does not violate the fairness conditions imposed by 
the jls, it is not quite what I expect given the perfectly fair behavior 
when no i/o is performed. Once again, this is not a bug in the blackdown 
vm since it still satisfy the jls, but the behavior of ibm's vm is more 
*consistent*.

-- dimitris
   mailto:[EMAIL PROTECTED]
public class test_fairness implements Runnable {

    int when_;
    int run_;
    int max_;

    static boolean active = true;

    test_fairness( int when, int max ) { 
        when_ = when; 
        max_ = max;
    }
 
    public void run() {
        while( active = (run_++ < max_) ) {
            for ( int i = 0; i < when_; i++ );
            Thread.yield();
        }
    }
    
    public static void main( String[] args ) throws InterruptedException {
        int when = args.length > 0 ? 
            Integer.parseInt( args[0] ) : 100;
        int max = args.length > 1 ? 
            Integer.parseInt( args[1] ) : 1000000;
        
        test_fairness t1, t2, t3;
        Thread th1, th2, th3;
        (th1 = new Thread( t1 = new test_fairness( when, max ) )).start();
        (th2 = new Thread( t2 = new test_fairness( when, max ) )).start();
        (th3 = new Thread( t3 = new test_fairness( when, max ) )).start();
        
        th1.join();
        th2.join();
        th3.join();
        
        System.out.println( "t1: " + t1.run_ 
                            + " t2: " + t2.run_ 
                            + " t3: " + t3.run_ );

    }

}

Reply via email to