A Guide To The Java Executor Service Baeldung

User Manual: Pdf

Open the PDF directly: View PDF PDF.
Page Count: 7

Scroll down to view the document on your mobile browser.
3/30/2018 A Guide to the Java ExecutorService | Baeldunghttp://www.baeldung.com/java-executor-service-tutorial 1/11A Guide to the JavaExecutorServiceLast modied: January 26, 2018by baeldung (http://www.baeldung.com/author/baeldung/)Java (http://www.baeldung.com/category/java/)I just announced the new Spring 5 modules in REST With Spring:>> CHECK OUT THE COURSE (/rest-with-spring-course#new-modules)1. OverviewExecutorService(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) is aframework provided by the JDK which simplies the execution of tasks in asynchronous mode.Generally speaking, ExecutorService automatically provides a pool of threads and APIforassigning tasks to it. Further reading:Guide to the Overview of the Guide to+ (http://baeldung.com)
3/30/2018 A Guide to the Java ExecutorService | Baeldunghttp://www.baeldung.com/java-executor-service-tutorial 2/11Fork/Join Frameworkin Java(http://www.baeldung.com/java-fork-join)An intro to the fork/joinframework presented in Java 7and the tools to help speed upparallel processing byattempting to use all availableprocessor cores.Read more(http://www.baeldung.com/java-fork-join) java.util.concurrent(http://www.baeldung.com/java-util-concurrent)Discover the content of thejava.util.concurrent package.Read more(http://www.baeldung.com/java-util-concurrent) java.util.concurrent.Locks(http://www.baeldung.com/concurrent-locks)In this article, we explorevarious implementations ofthe Lock interface and thenewly introduced in Java 9StampedLock class.Read more(http://www.baeldung.com/java-concurrent-locks) 2. InstantiatingExecutorService2.1. Factory Methods of the Executors ClassThe easiest way to create ExecutorService is touse one of the factory methods of theExecutorsclass.For example, the following line of code will create a thread-pool with 10 threads:The are several otherfactorymethods to create predened ExecutorServicethat meet specicuse cases. To nd the best method for your needs, consult Oracle’s ocial documentation(https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html).2.2. Directly Create anExecutorServiceBecauseExecutorServiceis an interface, an instance of any its implementations canbe used.There are severalimplementations to choose fromin the java.util.concurrent(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html) package oryou can create your own.For example, theThreadPoolExecutor class has a few constructors which can be usedtocongure an executor service and its internal pool.1 ExecutorService executor = Executors.newFixedThreadPool(10);

Navigation menu