The Term behind this concept is called interceptor. You need a container that does this like a ApplicationServer, CDI Container, Spring or AOP. It works like this
1. call your Method
2. pause your Method
3. call intercepter
4. do interceptor stuff
5. resume your Method inside the interceptor