title: What is the most optimal way to obtain a value from a thread and use it in the main thread right after the thread resolves?
body: <p>I'm making an android app that scrapes a website and puts the scraped information into a list. The issue that I have is that I have to block the main thread for some time until the scraping is done.</p>
  <pre><code>int opt = 0;
  String phone = &quot;&quot;;
  
  ...
  
  adAdapter = new AdAdapter(new AdDiffer(), ad -&gt;
  {
  
      new Thread(new Runnable()
          {
              @Override
              public void run()
              {
                  try
                  {
                      Document doc = Jsoup.connect(ad.getUrl()).get();
  
                      Element elem = doc.getElementsByClass(&quot;hGYViW&quot;).get(0).....
  
                      Elements e = null;
  
                      try
                      {
                          e = elem.getElementsByTag(&quot;div&quot;).get(0).getElementsByClass(&quot;hMvGEj&quot;);
                      }
                      catch (Exception ex) {}
  
                      if(e.size() != 0)
                      {
                          phone = elem.getElementsByTag(&quot;div&quot;).get(0).getElementsByClass(&quot;hMvGEj&quot;).get(0).getElementsByTag(&quot;a&quot;).get(0).html();
                          opt = 2;
                      }
                      else
                      {
                          opt = 1;
                      }
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
  
          }).start();
  
          while(opt == 0) {}
  
          if(opt == 2)
          {
              opt = 0;
              phone = &quot;&quot;;
              Intent intent = new Intent(Intent.ACTION_CALL);
              intent.setData(Uri.parse(&quot;tel:&quot; + phone));
              startActivity(intent);
          }
          else
          {
              opt = 0;
              phone = &quot;&quot;;
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(ad.getUrl())));
          }
          return null;
  });
  </code></pre>
  <p>Is there a way to make this work without having to block the main thread?</p>