title: java.lang.IllegalMonitorStateException: object not locked by thread before wait()?
body: <p>I am using ProgressDialog. I need to stop the thread when a user closes the ProgressDialog. Unfortunately, it is giving an exception.</p>
  <p>In inner class:</p>
  <pre><code>class UpdateThread extends Thread{
  
      public  void run() {
          while (true){
              count=adapter.getCount();
  
              try {
                 mHandler.post(  new Runnable() {
                      public  void run() {
                          Log.i(TAG,count+&quot;count&quot;);
                          progressDialog.setMessage(count + &quot;Device  found&quot;);
                      }
                  });
                  Thread.sleep(300);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  <p>Oncreate:</p>
  <pre><code> updateThread=new UpdateThread();
  
   progressDialog= new ProgressDialog(GroupListActivity.this);
   synchronized (this) {
       updateThread.start();
   }
  </code></pre>
  <p>Ondismissal:</p>
  <pre><code>   progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
          @Override
          public  void onDismiss(DialogInterface dialog) {
              try {
                  synchronized (this) {
                      updateThread.wait(300);
                  }
  
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              Log.i(TAG,&quot;Thread is stopped&quot;);
          }
      });
  </code></pre>