Dead end with synchronized and scala flags

I cannot understand why the following code locks:

"Locks around blocking futures" should "be re-entrant" in {
    val lock = new Object()

    def processInFuture = {
      lock.synchronized {
        // simulate async call made blocking
        Await.result(Future {
          blocking {
            logger.info("Sleeping")
            Thread.sleep(100)
            logger.info("Waking")
          }
        }, Duration.Inf)
      }
    }

    // fire off 10 async events and wait on each one
    (0 until 10).
      map(_ => Future { processInFuture }).
      foreach(future => Await.result(future, Duration.Inf))
  }

I do not understand why creating asynchronous synchronization in a critical section causes the entire fork-join pool to work.

If I make the future executable in a separate pool from the fork connection pool, it will work. I do not understand why the fork bundle thread does not block other threads and then ends first. Is it because the pool is somehow blocked?

I understand that it is always better to do all async if its asynchronous, but some scripts do not allow this (e.g. caching with guava cache)

- EDIT

To illustrate @Dima's answer, this works

"Locks around blocking futures" should "be re-entrant" in {
    val lock = new Object()

    def processInFuture = {
      blocking { 
        lock.synchronized {
          // simulate async call made blocking
          Await.result(Future {
            blocking {
              logger.info("Sleeping")
              Thread.sleep(100)
              logger.info("Waking")
            }
          }, Duration.Inf)
        }
      }
    }

    // fire off 10 async events and wait on each one
    (0 until 10).
      map(_ => Future { processInFuture }).
      foreach(future => Await.result(future, Duration.Inf))
  }
+4
source share
1 answer

, . , 8. .

( ). . ( , , ).

, , . , .

, , .

: 7 , , 8.

, , , .

7 , , , , , .

- > .

. : - blocking processInFuture. , .

+6

Source: https://habr.com/ru/post/1655543/


All Articles