Assume you have a jelly tag <j:jelly> with children. The following sequence leads to concurrency problems:
1. Instance 1 of Tag is created on thread 1. Body is ScriptBlock
2. Instance 2 of Tag is created on thread 2. Body is same ScriptBlock as Instance 1.
3. doTag -> invokeBody -> getBody -> trimBody is called for Instance 1
4. same for instance 2
5. Instance 1 gets the size of the ScriptBlock
6. Instance 2 gets the size of the ScriptBlock
7. Instance 1 trims whitespace blocks
8. Instance 2 gets an IndexOutOfBounds exception since it resumes execution with an incorrect list size (list was changed by Instance 1).
Both threads concurrently trim the same ScriptBody (the hasTrimmed variable is instance-specific). The attached patch synchronizes access to the body within trimBody.