[SOLVED] Using `Future` combined with `synchronized`

How about you refactor it to a lazy future?

lazy val extractedPath: Future[Path] = extract(zipFile)

The first time you access it, it will trigger extract, but for all consecutive calls the path will be already extracted. Personally, I wouldn’t worry about a case when two first calls happen so quickly that there’s a risk they will trigger extract twice. If it’s really important, you can wrap the call in synchronized: lazy val extractedPath: Future[Path] = synchronized { extract(zipFile) } but I would first ensure if this is needed.

3 Likes