title: Run a thread in android after one thread completes its execution
body: <p>I want run run a thread after one thread completes its execution.</p>
  
  <p>Here I am using progress bar, after progress bar completes the method do1() should execute but when I am run the application the application force close.</p>
  
  <p>here is my code..</p>
  
  <pre><code>         public void onenc(View view) {
            progressBar = new ProgressDialog(view.getContext());
          progressBar.setCancelable(true);
          progressBar.setMessage("Ecoding Text ...");
          progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          progressBar.setProgress(0);
          progressBar.setMax(100);
          progressBar.show();
  
          //reset progress bar status
          progressBarStatus = 0;
  
          //reset filesize
          fileSize = 0;
  
  
          new Thread(new Runnable() {
            public void run() {
              while (progressBarStatus &lt; 100) {
  
                // process some tasks
                progressBarStatus = doSomeTasks();
  
                // your computer is too fast, sleep 1 second
                try {
                  Thread.sleep(10);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
  
                // Update the progress bar
                progressBarHandler.post(new Runnable() {
                  public void run() {
                    progressBar.setProgress(progressBarStatus);
                  }
                });
              }
  
              // ok, file is downloaded,
              if (progressBarStatus &gt;= 100) {
  
                  // sleep 2 seconds, so that you can see the 100%
                  try {
                      Thread.sleep(2000);
  
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
                  // close the progress bar dialog
                  progressBar.dismiss();
  
              }
  
  
            }
             }).start();
  
          Thread tt =new Thread(new Runnable() {
                public void run() {
          do1();
  
           try {
                  Thread.sleep(1100);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
  
                }
             });
          tt.start();
  
        }
  </code></pre>
  
  <p>I have also tried AsyncTask but both run simultaneously.</p>
  
  <p>Please help me I am a newbie in android.</p>
  
  <p>Thanks in advance. </p>
  
  <p>Update:</p>
  
  <p>After AsyncTask</p>
  
  <pre><code>      class MyFirstTask extends AsyncTask&lt;String, Boolean, Boolean&gt; {
              @Override
              protected Boolean doInBackground(String... params) {
                  return null;
                  //Do Stuff
              }
             public void progressUpdate(Integer progress) {
                 new Thread(new Runnable() {
                        public void run() {
                          while (progressBarStatus &lt; 100) {
  
                            // process some tasks
                            progressBarStatus = doSomeTasks();
  
                            // your computer is too fast, sleep 1 second
                            try {
                              Thread.sleep(10);
                            } catch (InterruptedException e) {
                              e.printStackTrace();
                            }
  
                            // Update the progress bar
                            progressBarHandler.post(new Runnable() {
                              public void run() {
                                progressBar.setProgress(progressBarStatus);
                              }
                            });
                          }
  
                          // ok, file is downloaded,
                          if (progressBarStatus &gt;= 100) {
  
                              // sleep 2 seconds, so that you can see the 100%
                              try {
                                  Thread.sleep(2000);
  
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
  
                              // close the progress bar dialog
                              progressBar.dismiss();
  
                          }
  
  
                        }
                         }).start();
             }
             @Override
             protected void onPostExecute(Boolean result) {
                 //Call your next task
                 Thread tt =new Thread(new Runnable() {
                    public void run() {
              do1();
  
               try {
                      Thread.sleep(1100);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
  
                    }
                 });
              tt.start();
             }
        }
  </code></pre>
  
  <p>Now not even a single thread is executing the application force closes</p>
  
  <p>After Another Update</p>
  
  <pre><code>            class MyFirstTask extends AsyncTask&lt;String, Boolean, Boolean&gt; {
              @Override
              protected Boolean doInBackground(String... params) {
                    new Thread(new Runnable() {
                        public void run() {
                          while (progressBarStatus &lt; 100) {
  
                            // process some tasks
                            progressBarStatus = doSomeTasks();
  
                            // your computer is too fast, sleep 1 second
                            try {
                              Thread.sleep(10);
                            } catch (InterruptedException e) {
                              e.printStackTrace();
                            }
  
                            // Update the progress bar
                            progressBarHandler.post(new Runnable() {
                              public void run() {
                                progressBar.setProgress(progressBarStatus);
                              }
                            });
                          }
  
                          // ok, file is downloaded,
                          if (progressBarStatus &gt;= 100) {
  
                              // sleep 2 seconds, so that you can see the 100%
                              try {
                                  Thread.sleep(2000);
  
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
  
                              // close the progress bar dialog
                              progressBar.dismiss();
  
                          }
  
  
                        }
                         }).start();
  
                  return null;
                  //Do Stuff
              }
             public void progressUpdate(Integer progress) {
  
             }
             @Override
             protected void onPostExecute(Boolean result) {
  
                 do1();
                 //Call your next task
          /*     Thread tt =new Thread(new Runnable() {
                    public void run() {
              do1();
  
               try {
                      Thread.sleep(1100);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
  
                    }
                 });
              tt.start();*/
             }
        }
  </code></pre>
  
  <p>Now the function do1() executes before progress bar completes.</p>
  
  <p><strong>Finally Solved it....Here is the answer.
  Hope it will help others</strong></p>
  
  <pre><code>         class MyFirstTask extends AsyncTask&lt;String, String, String&gt; {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressBar.show();
  
  
            }  
            @Override
              protected String doInBackground(String... params) {
  
  
                int i=0;
                  while(fileSize&lt;100)
                  {
                      fileSize=fileSize+1;
                       publishProgress(""+(int)(fileSize));
                      try {
                          Thread.sleep(10);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }                       
                  }
  
                   publishProgress(""+(int)(fileSize));
  
                  return null;
  
  
              }
             public void onProgressUpdate(String... progress) {
                 progressBar.setProgress(Integer.parseInt(progress[0]));
             }
             @Override
             protected void onPostExecute(String result) {
                 progressBar.dismiss();
                 do1();
  
  
             }
        }
  </code></pre>