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]

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

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路