Concurrency in Java Stack Training

This training covers Java concurrency basics.


  • A working understanding of Java
  • Java JDK 1.6+
  • Experience with Java Stack application development
  • Java Concurrency

Slide deck

Example code

Lab 1

Project setup

  • Download the example code and import the project into your favorite IDE.
  • Get your application deployed and running in Tomcat.
  • Go to http://localhost/concurrency/lab1, this should take a few seconds to load.

Run Fibonacci methods asynchronously

  • Open the applicationContext.xml file and add:
<stack-concurrency:fixed-thread-pool />

This will add an ExecutorService instance to your Spring context.

  • Inject the ExecutorService into the Lab1 class by adding the following to
private ExecutorService executorService;
  • Create a method such as the following that invokes the fib(int n) method using the ExecutorService:
public Future<Long> submitFig(final int n) {
	return executorService.submit(new Callable<Long>() {
		public Long call() throws Exception {
			return fib(n);
  • Now, instead of invoking fib directly multiple times, use the submitFig method. Something like the following:
final Future<Long> future1 = submitFig(40);
final Future<Long> future2 = submitFig(41);
final Future<Long> future3 = submitFig(42);
  • We can let the Fibonacci get calculated in the background (since we don't do anything with the results as this is a contrived example.) This will dramatically improve the speed of the Lab 1 code. Or, we can invoke the get() method on each of the futures to retrieve the value.

Lab 2

  • Open the applicationContext.xml file and modify your fixed-thread-pool so that it uses just a few threads and has a small queue capacity. Something like the following will work nicely:
<stack-concurrency:fixed-thread-pool pool-size="2" queue-capacity="5" />
  • Restart Tomcat and go to the following URL: http://localhost/concurrency/lab2
  • It should error out because the thread-pool will have exceeded its capacity. If you open, you will see that it's trying to queue up 10 tasks for execution.
  • Change your fixed-thread-pool configuration so that it has a rejection-policy of CALLER-RUNS.
  • Restart Tomcat and try the URL again. It should succeed. Observe in the console how executor threads and web threads are now being used.
  • Change the rejection-policy to DISCARD and notice how all of the tasks no longer get executed.

Lab 4

  • Open the applicationContext.xml and add a scheduler and a task:scheduled-tasks similar to the following:
<stack-concurrency:scheduler-thread-pool id="scheduler" />

<task:scheduled-tasks scheduler="scheduler">
	<task:scheduled ref="backgroundService" method="background" fixed-rate="5000"/>
  • Now start Tomcat. You should see in the console the string "This is a background task." getting logged to the console every 5 seconds.
  • Go back to applicationContext.xml, remove the task:scheduled-tasks config and replace it with
<stack-concurrency:scheduler-thread-pool id="scheduler" />

<task:annotation-driven scheduler="scheduler" />
  • Open and annotate the background() method with @Scheduled(fixedRate = 5000)
  • If you restart Tomcat now, you will notice that nothing happens. This is because the backgroundService bean hasn't been initialized.
  • If you modify the backgroundService bean config in applicationContext.xml and add the following attribute: lazy-init="false"
  • Now restart Tomcat and you will again see the string "This is a background task." getting logged to the console.
This page was last modified on 16 August 2012, at 14:08.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.