Java EE 7 – Concurrency Utilities
Join the DZone community and get the full member experience.
Join For Freeconcurrency utilities ( jsr 166 ) is a new java ee standard that comes with the java ee 7 specification. this standard is about the presentation of executor api which was introduced with java 1.5 as container-side and managed objects.
executor api is located under the high level concurrency objects title in the java se documents which are presented by oracle. executor api provides thread management efficiently through the objects it offers. in general, this efficiency is provided by a several of thread pool mechanisms. .
thread pooling
thread pools contain creating a specified number of worker thread and running of the user-defined tasks with these worker threads. in this way, maximum running user-defined tasks are limited by pool size and system resources are being used efficiently.
the figure above can help you understand thread pools better. for example on the left side, it is wanted to execute 5000 pieces of tasks on your application with for loop. trying to activate these 5000 pieces of tasks without using thread pool will threaten the system resources. we can understand better this threat issue by looking at the number of tasks (thread) that a running application in your system activates. .
for example, this one shows a series of processes and a number of threads that it manages which are executed in my machine. it can be seen clearly that idea.exe application has 71 pieces of threads. when you look at other applications, you can see that less thread is being used. so, if i have 5000 pieces of tasks and fork these 5000 pieces of tasks as 5000 different threads, does it mean that i set up a good architecture? (of course not : )
if, in fact, i make run the tasks by making choices (random or priority) from these 5000 tasks with the specified number of worker threads, i use the system resources efficiently and do not put the system in an unnecessary stress. because 5 pieces of worker thread is defined in the above chart, the effect of these tasks to the system it runs will be seen at most 5 user-defined tasks. when a worker thread finishes, one of the currently waiting task will be selected and this running process will continue until all tasks finish. so, the basic philosophy of executor api that comes with java se 1.5 is to provide efficient thread management by offering a variety of thread pool environments. worker threads are the common type of threads that are generated by thread factory.
the basic interface types within the executor api are executorservice and scheduledexecutorservice. the implementation of these interfaces may be a various of static methods of executor s class.
the new concurrency utilities standard that comes with java ee 7 makes these object types injectable and manageable by container services. however, container managed executor objects appear with the names of managed executorservice and managed scheduledexecutorservice in terms of representing the manageability. when you look at the uml diagrams of these interfaces, it can be seen that java se executor api interfaces are extended .
interfaces which start with managed basically provide the same operations that the executor api components provide which comes with java 1.5. the difference here is to present the new objects as container resources to the developers.
container resources are special objects that are managed by application servers. datasources, jms resources and the concurrency units with the concurrency utilities standard are the examples of container resources.
container resources are the objects that reside on application server and they perform specific functions. the access to these objects can be established by jndi (java naming and directory interfaces) standard. this access operation can be achieved basically through the @resource annotation or the context interface type objects (e.g. initialcontext).
creating concurrency resources
manageable executor objects within the concurrency utilities can be created on application servers which supports java ee 7 specification. for example, this creating process can be defined visually through the asadmin command set for glassfish 4 or over the glassfish admin console.
the asadmin tool in the /bin directory of glassfish application server is being used for command line operations. in the following example, the asadmin tool is run and manageable executor objects are created interactively with create-managed-executor-service or create-managed-scheduled-executor-service commands. because the container resources are provided to application environments with the access expressions which were defined in jndi standard, the unique access identifier which represents that container resource should be entered to console.
these created manageable executor objects can be seen through the glassfish admin console. similarly, these adding and editing operations can be done through the admin console .
container resources on glassfish 4 appear under the resources tab. concurrent resources appear under a sub-tab, concurrent resources . while managedexecutorservice and scheduledmanagedexecutorservice resources that we created it as manageable by container are found in this part, concurrency resources with __default prefix which were defined immanently on application server are also available. if desired, existing default resources can also be used.
getting resources of concurrency utilities
concurrency resources that reside on application server can be obtained through @resource annotation with injection or through the initialcontext objects. the @resource annotation and initialcontext object provide standard jndi access not only to concurrency resources, but also to all other container resources.
@webservlet(urlpatterns = "/kodcu",name = "kodcuservlet") public class kodcuservlet extends httpservlet { @resource // (1) private managedexecutorservice defaultmanagedexecutorservice; @resource // (2) private managedscheduledexecutorservice defaultscheduledexecutorservice; @resource(lookup = "concurrent/kodcuexecutor") // (3) private managedexecutorservice managedexecutorservice; @resource(lookup = "concurrent/kodcuscheduledexecutor") // (4) private managedscheduledexecutorservice scheduledexecutorservice; @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { ... initialcontext context=new initialcontext(); // (5) managedexecutorservice managedexecutorservicewithcontext = (managedexecutorservice) context.lookup("concurrent/kodcuexecutor"); ... } }
in the above servlet class, the default concurrency resources (number 1 and 2) and the resources which were created by specifying jndi name in the command line (number 3 and 4) are being obtain through @resource annotation with resource injection. the lookup field of @resource annotation gets the relevant object of unique resource access identifier which was defined with jndi standard. when @resource annotation is used without lookup attribute, jndi resources with __default prefix are being injected. in section 5, except injection method, concurrency resource is obtained according to relevant jndi name through the initialcontext object.
you can access to example in the following link
http://en.kodcu.com/2013/10/java-ee-7-concurrency-utilities-spesification/
by for now.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Use an Automatic Sequence Diagram Generator
-
The Dark Side of DevSecOps and Why We Need Governance Engineering
-
The Role of Automation in Streamlining DevOps Processes
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
Comments