Java Thread Yield() method example






Thread  yield()    method  in java  quits execution.  This  java program example this . This
execution  will  continuous  means   that  single  Thread  will  continuously  complete the
loop .   






class  Producer  extends Thread  
{  
  
public  void  run() {
for( int i=0 ; i<10 ; i++) {
Thread.yield();
System.out.println(" Produced");
}
  }
}

 class  Consumer  extends Thread  
{  
  

public  void  run() {
for( int i=0 ; i<10 ; i++) {
Thread.yield();
System.out.println(" Consumed");
}
}
}



public  class YieldAndRun {
    
    public static void main(String[]args)  
    {  
    Producer  t1 = new Producer ();  
    Consumer t2 = new Consumer();  
        // this will call run() method  
    
    // t2.setPriority(Thread.MAX_PRIORITY);
    
      t1.start();
      t2.start();  
    
    
          // System.out.println(Thread.currentThread().getName() + " in control");  
        
    }  
    
     
    
  
}


Program Output  :  You  can see the out . Further clarity  you can 
 modify the code for synchronous output.  

 Produced
 Produced
 Produced
 Produced
 Produced
 Produced
 Consumed
 Consumed
 Produced
 Consumed
 Consumed
 Produced
 Produced
 Produced
 Consumed
Java Thread Yield() method example Java  Thread  Yield()   method example Reviewed by Mukesh Jha on 1:08 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.