title: How the caller thread wait till task under ScheduledExecutorService finish the job periodicaly
body: <p>I have a requirement like Every 45 minute value has to be updated after making http call.</p>
  <pre><code>import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.CloseableHttpResponse;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.HttpClients;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.util.EntityUtils;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.node.ObjectNode;
  
  public class TokenManagerRunnable implements Runnable{
      
      String token;
  
      
      public String fetchToken() {
          return this.token;
      }
      
  
      @Override
      public void run() {
          
          String result = &quot;&quot;;
          
          HttpPost post = new HttpPost(&quot;https://login.microsoftonline.com/{{TenentId}}/oauth2/token&quot;);
          List&lt;NameValuePair&gt; urlParameters = new ArrayList&lt;&gt;();
          urlParameters.add(new BasicNameValuePair(&quot;grant_type&quot;, &quot;client_credentials&quot;));
          urlParameters.add(new BasicNameValuePair(&quot;client_id&quot;, &quot;some client id&quot;));
          urlParameters.add(new BasicNameValuePair(&quot;client_secret&quot;, &quot;some secret id&quot;));
          urlParameters.add(new BasicNameValuePair(&quot;resource&quot;, &quot;https://database.windows.net&quot;));
  
          try {
              post.setEntity(new UrlEncodedFormEntity(urlParameters));
          } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          try (CloseableHttpClient httpClient = HttpClients.createDefault();
               CloseableHttpResponse response = httpClient.execute(post)){
  
              result = EntityUtils.toString(response.getEntity());
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          
          ObjectNode node;
          try {
              node = new ObjectMapper().readValue(result, ObjectNode.class);
          
          
          System.out.println(result);
          
          if (node.has(&quot;access_token&quot;)) {
              result = node.get(&quot;access_token&quot;).toString();           
          }
          System.out.println(result);
          System.out.println(result.substring(1, result.length()-1));
          
          
          //updating the token
          this.token = result.substring(1, result.length()-1);
          
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  <p>And here is my main function</p>
  <pre><code>        SQLServerDataSource ds = new SQLServerDataSource();
          TokenManagerRunnable tokenManagerRunnable = new TokenManagerRunnable();
          ScheduledExecutorService sches = Executors.newScheduledThreadPool(1);
          sches.scheduleWithFixedDelay(tokenManagerRunnable, 0, 45, TimeUnit.MINUTES);
          System.out.println(&quot;fetching the token ---- &quot;+tokenManagerRunnable.fetchToken());
          ds.setAccessToken(tokenManagerRunnable.fetchToken());
         try {
          
          Connection connection = ds.getConnection(); 
          Statement stmt = connection.createStatement();
          ResultSet rs = stmt.executeQuery(&quot; select * from [dbo].[CLIENT]&quot;); 
          System.out.println(&quot;You have successfully logged in&quot;);
             
          while(rs.next()) {
              System.out.println(rs.getString(1));
          }
          
      }catch(Exception ex) {
          ex.printStackTrace();
      }
  </code></pre>
  <p>tokenManagerRunnable.fetchToken() brings null as the TokenManagerRunnable class is not yet executed.</p>
  <p>How can we achieve wait till  ScheduledExecutorService complete the task so we can get the value and set the new value in Datasource after every 45 minutes from tokenManagerRunnable.fetchToken() instead of null?</p>
  <p>Any thoughts?</p>