multithreading - submit method of Java concurrent framework -


new submit method of java concurrent framework. wondering whether thread got executed when call submit or when call get? researched oracle official document, cannot find information. thanks.

i referring sample below,

http://www.vogella.com/tutorials/javaconcurrency/article.html

package de.vogella.concurrency.callables;  import java.util.arraylist; import java.util.list; import java.util.concurrent.callable; import java.util.concurrent.executionexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future;  public class callablefutures {   private static final int nthreds = 10;    public static void main(string[] args) {      executorservice executor = executors.newfixedthreadpool(nthreds);     list<future<long>> list = new arraylist<future<long>>();     (int = 0; < 20000; i++) {       callable<long> worker = new mycallable();       future<long> submit = executor.submit(worker);       list.add(submit);     }     long sum = 0;     system.out.println(list.size());     // retrieve result     (future<long> future : list) {       try {         sum += future.get();       } catch (interruptedexception e) {         e.printstacktrace();       } catch (executionexception e) {         e.printstacktrace();       }     }     system.out.println(sum);     executor.shutdown();   } }  

no, calling submit doesn't execute task. , calling doesn't execute task. task processed asynchronously in thread belonging executor's threadpool.

calling submit method gives task threadpool. task queued until 1 of threads in pool free. pool assigns task worker thread, invokes code.

when call future.get, either worker has finished processed task, worker processing task, or task still queued waiting worker become available. if task done future returns value returned callable task, otherwise calling future's method causes main thread block until task finished.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo