Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

yield() method  RSS feed

 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.event.*;
class mythread extends Frame implements
Runnable,ActionListener {
int x=0,y=0,x1=0,x2=0,x3=0;String nm;boolean flag=true;
public void run(){
for ( int c=0;c<800 ;c++ ) call();
}
mythread(){
Button bt=new Button("Start");
add("South",bt);
bt.addActionListener(this);
setBounds(0,0,800,130);
setVisible(true);}
public void update(Graphics g ){ g.drawString("|",x,y);
release();}
public void paint(Graphics g){update(g);}

public static void main(String[]au){
mythread mt=new mythread();
}
public void actionPerformed(ActionEvent ae){
Thread t1=new Thread(this,"1");
Thread t2=new Thread(this,"2");
Thread t3=new Thread(this,"3");
t1.setPriority(2);
t2.setPriority(6);
t3.setPriority(10);//even i kept all //priorities same can't see any differece
//using yield as below
t1.start();
t2.start();
t3.start(); }

synchronized void call(){
while ( flag )
try { wait();
}
catch(InterruptedException ie){}
String nm=Thread.currentThread().getName();
int sw=Integer.parseInt(nm);
switch(sw){
case 1 :{ x1++ ; y=40;x=x1;// no change when // used Thread.yield();here
break; }
case 2 :{ x2++; y=60; x=x2;
break; }
case 3 :{ x3++; y=80;x=x3;
break;}
}
flag=true; repaint();
try{Thread.sleep(1); }
catch(InterruptedException ie){}
}
synchronized void realease(){
flag=false;
notify();
}
}
Threads only yeild the cpu to other threads of same priority.Right?
the above codes I can't see a useful use of yield method.
can someone suggest me a use of it so that it gets clear in my mind.also tell me if I used anything wrongly specially synchronized and wait/notify metods.
thanks
Nasir
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nasir.yield() is a static method of the thread class which will put the currently executing Thread into the Waiting state IF THE SCHEDULER IS WILLING TO RUN ANY OTHER THREAD.Normally this will happen if the other thread is of a higher priority than the currently executing thread.Note that most schedulers DO NOT stop the current thread from executing in favour of a Thread of lower priority.
I was not able to get clearly where u used yield in ur code.If u can explain more clearly , i might be able to help u.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
anrup kris
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nasir
You are right.Threads only yield to a thread of same or higher priority.The yield() method is a way to allow a thread to voluntarily give up the execution to other equal or higher priority threads especially in pre-emptive operating systemsIn non pre-emptive o/p systems,the threads will anyway be executed in round robin fashion,so that no thread gets the cpu for the entire time.
From what I understood,you are trying to use the yield() method on t1 in call() method definition.
But t1 has been defined with the lowest priority of 2.So it will anyway be getting the cpu only after t2 and t3 give up their lock on the object.
I ran your code after setting t1 and t3 to priority 5 and t2 to 10,and invoking yield on t1. The output shows that t1 runs much slower than t3,because it yields the cpu to t3,whenever t2 is sleeping and the contention is between t1 and t3 for the lock of the object.This is the only visible way I can see of demonstrating the yield() method's behaviour.

regds
kris
[This message has been edited by anrup kris (edited November 11, 2000).]
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Anrup and Udayan,
Thanks for reply
As far I know Yeild only works when priorities of all threads
set to the same vlaue. With my code I set priorities of t1,t2 and t3 to same value(eg.5) and then used yeild method in case1's
block .I expected that t1 would always give up cpu for other threads and would run slower itself but couldn't see such a thing.
Nasir
[This message has been edited by Nasir Khan (edited November 21, 2000).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!