xgtandyx 2010-03-07 14:49
浏览 241
已采纳

关于同步锁的问题,请教各位大牛··(急~~)

小弟在做一个一个程序的时候,希望主程序执行到一定代码时,启动一个线程,并等待主程序的一个标识符变化时唤醒,进而执行之后的代码,程序代码如下:
出现的问题是系统提示我没有上锁,这是为什么呢?请教各位大牛···

public class TimerTest extends Activity implements Runnable{

int num=0; 
 Thread read=new Thread(this); 
 int[] timerlenth={5000,6000,2000,4000}; 
 int[] number={1,2,3,4}; 
 Handler handler; 
             TimerTask task; 
 ViewFlipper vf; 
 Timer timer = new Timer(); 
  /** Called when the activity is first created. */ 

public Handler createHandle() 
 { 
 Handler handler = new Handler(){    
 public void handleMessage(Message msg) {    
 switch (msg.what) { 
      case 1:  

          vf=(ViewFlipper)findViewById(R.id.ViewFlipper01); 
          num=1; 

   break;            

      super.handleMessage(msg);    
  }    

 }; 
return handler; } 

 public TimerTask createTimerTask(final int i,final Handler handler) 
{ 
 TimerTask task = new TimerTask(){    

      public void run() {    
            Message message = new Message();        
             message.what = i;        
             handler.sendMessage(message);      
         }    

     }; 
return task; 
} 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //timer.schedule(task, 5000); 
    //for(int i=0;i<timerlenth.length;i++) 
    //{     

    handler=createHandle(); 
       task= createTimerTask(number[0], handler); 
    timer.schedule(task,timerlenth[0]); 
     try { 
        Go(read,num); 
    } catch (InterruptedException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    } 
        //vf=(ViewFlipper)findViewById(R.id.ViewFlipper01); 

        /**/ 
        vf.startFlipping(); 
        try { 
            Thread.sleep(10); 
        } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        vf.stopFlipping(); 


   // } 

} 
@Override 
public  void run() { 
    // TODO Auto-generated method stub       
} 
public synchronized void Go(Thread a,int b) throws InterruptedException    {//这个方法是设定线程锁的 
      while(b==0) 
      { 
                a.wait();     
      } 
      vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_in1)); 
       vf.setOutAnimation(AnimationUtils.loadAnimation(this,                    R.anim.push_out1)); 
       num=0; 
       notify(); 
} 
  • 写回答

1条回答

  • iteye_20112 2010-03-07 21:14
    关注

    [code="java"]
    public synchronized void Go(Thread a,int b) throws InterruptedException {//这个方法是设定线程锁的
    while(b==0)
    {
    a.wait();

    }
    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_in1));
    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_out1));
    num=0;
    notify();
    } [/code]

    这个函数,有问题。
    首先 a.wait() 要能正常运行,必须要这样
    [code="java"]synchronized (a) { a.wait(); } [/code],
    你这里语义有问题, a.wait() 的意思不是让a对应的线程等待,调用 Go 函数的线程才会等待。

    你这样写并不得到你预期的结果。

    ------------------------

    可以通过如下方式实现你要的结果。

    在 a Thread 的run方法中,作如下判断:
    [code="java"]
    while(b==0) { //前提,你必须要正确获得 b 的真正引用,这里是示例,你要根据实际情况修改。
    synchronized (this) {
    wait();
    }
    }
    [/code]

    Go 可以这样实现:
    public synchronized void Go(Thread a,int b) throws InterruptedException {//这个方法是设定线程锁的
    /*
    while(b==0)
    {
    a.wait();

    }
    */
    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_in1));
    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_out1));
    num=0;
    if (b != 0) {
    synchronized (a) {
    a.notify();
    }
    }
    } [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?