April 2018
Intermediate to advanced
382 pages
10h 11m
English
The only way you should use threads in an enterprise context, and if you really want to use it, is when the application server creates the thread. So here, we are kindly asking the container to do it using factory:
@Resource(name = "LocalManagedThreadFactory") private ManagedThreadFactory factory;
Using some functional-style code, we create our thread:
Thread thread = factory.newThread(() -> { response.resume(Response.ok(userBean.getUser()).build()); });
Now, moving to the managed stuff, we can set the name and priority of the just-created thread:
thread.setName("Managed Async Task"); thread.setPriority(Thread.MIN_PRIORITY);
And don't forget to ask the container to start it:
thread.start();
Read now
Unlock full access