public
class LettreT extends Thread
{
int nbf;
ChaineDeCaractere chaine;
String lettre;
public LettreT(int n,ChaineDeCaractere c,String l)
{
nbf=n;
chaine=c;
lettre=l;
}
public void run()
{
for (int i=0;i<10;i++)
{
synchronized(chaine)
{
for(int j=0; j<nbf;j++)
{
chaine.ajouter(lettre);
}
chaine.ajouter("_");
}
Thread.yield();
try{
sleep(1000);}
catch(InterruptedException e)
{e.printStackTrace();}
}
}
}
关于yield和sleep,小弟不是很懂。如果现在有两个线程,a线程执行完yield之后,继续执行sleep的还是a线程还是b线程???如果a线程暂停,代码里又没有notify方法,那a什么时候会被唤醒???先谢过!!
------解决方案--------------------
a不需要被唤醒。yield()不会释放锁标志,只是使当前线程重新回到可执行状态,所有执行yield()的线程有可能在进入到可执行状态后马上又被执行
这个链接里面有关于yield()的例子,可以看看
http://blog.csdn.net/wbchn/article/details/2462112
http://dylanxu.iteye.com/blog/1322066