[uberconf 2023] Structured Concurrency in Java

Speaker: Venkat Subramaniam


For more, see the table of contents


  • Incubator phase feature
  • Anything can change.
  • Will be a year or more before release
  • use –enable-preview flag
  • Java prints out warning using incubator feature whenever run program

Executor Service

  • Since Java 5
  • Executors.newFixedThreadPool(x)
  • executorService.submit() -> logic()) – returns future. Call future.get() when done
  • executorService.shutdown()
  • executorService.awaitTermination(10, TImeout.SECONDS)

Structured Concurrency

  • Allow control of what happens on parallel
  • Parent handles/processes failures from children
  • Currently import jdk.incubator.concurrent – will move
  • StructuredTaskScopeShutdownOnFailure – invoke all
  • StructuredTaskScopeShutdownOnSuccess – invoke any
  • try (var scope = new StructuredTaskScope()) – want autoclose – or scope = StructuredTaskScopeShutdownOnSuccess() or scope = StructuredTaskScopeShutdownOnSuccess<String>()
  • scope.fork(() -> logic()) – makes child tasks, still returns a Future
  • scope.join() – don’t use – better to use a timeout so not unbounded waiting on children
  • scope.joinUntil(Instant.now().plusSeconds(50)) – better, uses timeout. waits for all children up to timeout Nice using Java date math
  • future.resultNow() – get result immediately. (vs get() which waits if not available)
  • scope.throwIfFailed(ex- > handle()) – deal with first failure

Scoped Values

  • ThreadLocal not useful in Spring because thread executing code not the same one that created the value
  • Scoped value, not scoped variable. Immutable
  • Hide the value instead of changing it
  • New value available in inner value. When get back out of that scope, see original value.
  • Doesn’t matter which thread runs the code because matters where you are in code.
  • Useful when calling a lambda which does a callback via third party code
  • Created ScopedValue field in the class that needs the data.
  • ScopedValue.newInstance() – placeholder, doesn’t have value yet
  • state.isBound() – whether has been set to a value
  • ScopedValue.where(state, “test”).run(lamba) – binds the value
  • state.get() – get value if bound. Blows up if not bound

My take

As an incubator feature, I knew nothing about this. It was great to learn about it from a Venkat talk. Different then the usual which is something I do know about and am learning deeper. I also enjoyed the airport code jokes: IAH (Houston – i am here), IAD (Dulles – i am delayed), and ORD (Chicago – ordeal). I’m always impressed Venkat can live code in front of so many people. I’d never seen incubator code in use. Nice it gets a temporary package name; can’t use it by accident. Venkat used IntelliJ to create a module. I don’t think I’ve ever seen him use IntelliJ :). Great audience questions on structured task scope. The scoped value thing made my head swim for a bit, but I get it now.

[2022 javaone] hands on lab project loom

Facilitated by Oracle (Nicolai/Jose)

For more see the table of contents


  • The lab uses Oracle’s cloud environment. Setup was clear and well documented.
  • It’s a throwaway env only good for a few hours so you don’t have to worry about cleanup.
  • Can also do locally if prefer


  • Workshop: https://bit.ly/ocw22-hol3733
  • https://github.com/JosePaumard/2022_javaone-loom-livelab.git

Virtual Threads

  • A virtual thread runs on a “real” platform thread called a carrier thread
  • Can be mounted/dismounted from carrier thread when blocks
  • It’s cool seeing a platform thread run so many virtual threads

Structured Concurrency

  • I just skimmed this part (had ice cream and got distracted talking to people so didn’t do the code for this half.) Conveniently, it is in github and I can do it in the future. Probably when it makes it out of incubator so I don’t forget everything.

My take

It took almost half an hour to get started. The online editor doesn’t work in Mac Safari so did the setup twice. I could have used Java 19 locally, but wanted to see what the lab environment looked like. I had enough time though so it was ok.’

I got to use single file source code execution (the lab didn’t say to, but less typing.) Must tease someone about its usefulness. And I got to to try out Loom.

[2020 devnexus] synchronized is obsolete

Speaker: Enrique Zamudio

For more, seeĀ table of contents


  • AtomicInteger
    • incrementAndGet()
    • compareAndSet()
  • AtomicReference
    • updateAndGet()
  • ConcurrentHashMap – methods on Map, but happens atomically
    • computeIfAbsent()
    • merge()
  • ConcurentSkipListMap
  • ConcurrentSkipListSet
  • LinkedBlockingQueue
    • Can take elements and deliver elements concurrently.
    • Every element delivered to exaclty one consumer
    • Unblocks consumers as long as something in queue
  • Locks – Lock, ReadWriteLock, ReentrantLock, CountDownLatch, CyclicBarrier, Semaphore, Phaser
    • ReentrantLock – execute code if can acquire lock
    • lock()
    • tryLock()
    • tryLock(10, SECONDS)
    • unlock()
  • Akka – project that implements Actor paradigm
  • vert.x – project for reactive – do things in single thread
  • Project Loom – lightweight threads. I thread takes 100MB just to exist; Project Loom is more like 2KB. Also no context switching.

Multiple processes

  • multiple copies of your jar
  • multiple vms with load balancer
  • synchronized or lock fails because each process can run code at same time
  • Can use database select … for update in database transaction so outside your process.
  • Similarly can use redis to set a lock with a random value. If the value matches, you know the lock is yours. If JVM crashes, lock stays. Can add lock timeout to avoid this
  • FencedLock – developed by Hazelcast Raft consensus model.

My take

I feel like I re-learn this topic every time I read the chapter in our book. (Scott wrote the chapter). I’m glad I went to this session. The more times I hear this, the more sticks. I had forgotten the project loom info so was definitely good to hear that again. The part about multiple processes was mostly new to me. (I just new about select for update)