/* Illustrating why a thread may not execute because it has yielded.
   
   The low priority thread runs after the higher priority thread has
   yielded. Example doesn't work that great as Win95 time slices
         
*/

class Yielding {
    public static void main(String[] args) {
        MyThread t1 = new MyThread(1);
        MyThread t2 = new MyThread(2);
        
        t1.setPriority(Thread.NORM_PRIORITY);
        t2.setPriority(Thread.NORM_PRIORITY);   // won't yield() for lower priority
        t1.start();
        t2.start();
    }
}   

class MyThread extends Thread {
    int id;
    
    MyThread(int id) {
        this.id = id;    
    }
    
    public synchronized void run(){
        for(int i=0; i<100; i++) {
            if( id==1 && i==30 ) {
                    yield();
            }
            System.out.println("My id is: " + id);
        }
    }
}