2012-11-30 118 views
1

我正在处理我的应用程序中的线程,我面临线程等待函数的问题。我使用runOnUIThread()创建并显示了我的UI,我的主要thraed正在等待我的UI线程完成,在thraed完成后,我在主线程中执行了一些处理,问题在于主进程始终处于等待状态,即使我的线程完成; S的工作,我现在用的等待和notify()功能这个等待和通知在android

 public String LSVerification() { 

     String t = null; 

      t="Sample string"; 
      synchronized(this) 

      { 
      AndroidHTMLActivity.this.runOnUiThread(ShowDialog) ;//here i call my thread 

      try { 
       this.wait();//here i waiting for my thread to finish it's job 

      //here i do some process after my thread finish it's job 
       //the problem is here main process always in wait state 


       } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      } 
      return t;   
     } 

     private Runnable ShowDialog = new Runnable() { 
      public void run() { 
       String t = null; 
     synchronized(ShowDialog) 

      { 
      Recognition recg=new Recognition(Activity.this); 

       recg.show(); 


      this.notify(); 
      } 

      } 
     }; 
+0

我会syn chronize在同一个对象上的两个块,否则你可能有并发访问 – njzk2

+0

你调用this.notitify,这意味着你通知ShowDialog,而你在这等待着,这是另一个对象。使用1个通用对象来同步,通知和等待。 – njzk2

+0

即使我使用相同的物体,在等待状态下仍然没有发生主要过程 –

回答

1

尝试使用Hanlders执行在后台工作后更新UI ...

Link to Handlers见下面的代码..

private void performOperationInBackgroundThread() { 
     Thread Servicethread = new Thread(
        new Runnable() { 
         public void run() { 
          try { 
           PerformThreadOperation(); 
           DataLoaded = true; 
          } catch (Exception e) { 
           ExceptionOccured = true; 
           e.printStackTrace(); 
           System.out.println(e.getMessage()); 
          } 
          handler.sendMessage(handler.obtainMessage()); 
         } 
        } 
        ); 
      Servicethread.start(); 
     } 


static class MyHandler extends Handler{ 
       MyActiviy parentActivity; 
       @Override 
       public void handleMessage(Message msg){ 
        // Update your UI here 

        } 
       } 

       MyHandler(MyActiviy activity){ 
        parentActivity = activity; 
       } 
      } 

MyHandler handler = new MyHandler(this);