java.util.concurrent - java Futures execution -


I was reading about the promise in futures and javascript in java. Below is a code written as an example. I have a question that when the work that starts in the future is executed?

  1. When the future is built in the bottom line:
    Content Future = StartDownloading (New URL ("http://www.example.com")) ;

  2. Or when we call the obtain method last string content = content future.get ();

  3. Forcing the tax holder to call startdownloading ?

      Public Class Futures 1 {Personal Static Last Performance Service Pool = Examiner newFixedThreadPool (10); Public Stable Zero Main (string [] Args) {Future & lt; String & gt; Content Future = Faucet; {ContentFuture = startdownloading (Try the new URL ("http://www.example.com")); } Catch (malmarmdureleuxation E) {e.printStackTrace (); } // Try another calculation {Last String Content = Content Future.get (); } Hold (interrupted) exception. Execution Exception e) {e.printStackTrace (); }} Public stable future & lt; String & gt; Startdownloading {Last URL URL} submit (new callable & lt; string & gt; () {@ override public string call} throws exceptions {try (InputStream input = url.openStream ()) {IOUtils. ToString (input, Standard Chart) .UTF_8);}}}); }}  

But then why try calling me startdownloading in the block?

The exception is due to the creation of the download of the downloaded URL URL. If you have not downloaded the start and created the URL object, then it will be the same, that is;

  URL URL = FALL; Try {url = new URL ("http://www.example.com"); } Hold (Malformedlexception E) {// Hold the badly created URL. E.printStackTrace (); } If (url! = Null) Content Future = Start Downloading (URL);  

As you are downloading something, it may take a little longer (depending on size, speed, etc.), this will be the reason that you will be blocked on futures. Get () call - As long as anything that takes longer than downloads happens, in that case .get () will come back immediately (again only if download is completed before .get () Gone or it had to face exceptions).


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -