Skip to content

Month: November 2020

Execute Around idiom in Java

The Execute Around idiom is a pattern that allows you to wrap an action with some standard setup / tear down steps. Examples might include:

  • Execute within a lock: acquire the lock, do the action, release the lock
  • Resource handling: acquire the resource, do the action, close the resource
  • Execute as a user: switch to the user, do the action, switch back to the original user
  • Exception handling: do the action, handle the exceptions
  • Time an action: start a timer, do the action, stop the timer

The pattern allows you to pass in arbitrary actions and have them run with the same setup / tear down steps.