title: How to Stop/Wait a Thread and then Resume/Restart it later
body: <p>I am running a service in the background which is also binded to an activity to provide some data results to the view.
  So My service is running in the background and I am binding the service when my activity starts and unbinding it when it closes irrespective of my background service which is running constantly.
  And this service is first triggered when my activity starts the first time.</p>
  
  <p>Now I have created two threads in the service. One is used for the background work. Other thread "displayThread" is used to fetch and push data to the activity.</p>
  
  <p>Now my display thread is like this:</p>
  
  <pre><code>Thread displayThread = new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  executeDisplayQueue();
              } catch (InterruptedException e){
                  displayThread.interrupt();
              }
          }
  });
  </code></pre>
  
  <p>So when my activity starts my service starts, and I am starting both the threads:</p>
  
  <pre><code>@Override
  public void onCreate() {
      super.onCreate();
      Log.d(LOG_TAG, "Creating Service......");
      thread.start();
      displayThread.start();
      Log.d(LOG_TAG, "Service is created.");
  }
  </code></pre>
  
  <p>Now what I want is that when I am unbinding my service when activity closes my "displayThread" stops or pauses and it restarts or resumes when again activity starts which calls rebind() function.</p>
  
  <pre><code>@Override
  public boolean onUnbind(Intent intent) {
      super.onUnbind(intent);
      // Stop/Pause/Wait my displayThread here
      return true;
  }
  
  @Override
  public void onRebind(Intent intent) {
      super.onRebind(intent);
      // Restart/Resume my displayThread here
      Log.d(LOG_TAG, "trying to check display thread");
  }
  </code></pre>
  
  <p>Please help me here as I tried a lot of things.
  Now displayThread.wait() pauses my app screen and so it goes black as if everything is at halt which is certainly not the right way to do it.</p>
  
  <p>Please give me a solution as to how to proceed further.</p>
  
  <h1>EDIT</h1>
  
  <p>This is executeDisplayQueue() function:</p>
  
  <pre><code>@Override
      protected void executeDisplayQueue() throws InterruptedException {
          Log.d(LOG_TAG, "Starting execution of queue...");
          while (!Thread.currentThread().isInterrupted()) {
              SomeJob job = null;
              try {
                  job = jobShowQueue.take();
                  Log.d(LOG_TAG, "Taking Job no. " + job.getId() + " from the queue...");
                  if (job.getJobState().equals(JobState.NEW)) {
                      Log.d(LOG_TAG, "Job state is NEW.");
                      job.setJobState(JobState.RUNNING);
                      job.getCmd().run(bluetoothSocket.getInputStream(), bluetoothSocket.getOutputStream());
                  } else {
                      Log.e(LOG_TAG, "Job state is not NEW and therefore it should not be in Queue");
                  }
              } catch (InterruptedException ie){
                  Thread.currentThread().interrupt();
              } catch (UnsupportedCommandException u) {
                  if (job != null)
                      job.setJobState(JobState.NOT_SUPPORTED);
  
              } catch (IOException ioe){
                  job.setJobState(JobState.BROKEN_PIPE);
  
                  Log.e(LOG_TAG, "Broken Pipe Error. Close the connection and restart again: "+ioe.getMessage());
              } catch (Exception e){
                  job.setJobState(JobState.EXECUTION_ERROR);
                  Log.e(LOG_TAG, "Failed to run Command. Error: "+e.getMessage());
              }
              if (job != null){
                  Log.d(LOG_TAG, "Job is Finished.");
                  final SomeJob finalJob = job;
                  ((Activity) applicationContext).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          ((Activity) applicationContext).stateUpdate(finalJob);
                      }
                  });
              }
          }
      }
  </code></pre>
  
  <p>So basically I am taking jobs from the queue one by one and populating it in the activity. So when my activity closes I want this thread (displayThread) to stop as it no longer requires to populate anything. And when it opens again my displayThread should restarts itself.</p>