How to "mock" the StructuredTaskScope for integration tests where we want to run every task sequentially instead of concurrently

Daniel Andres Pelaez Lopez estigma88 at gmail.com
Fri Apr 11 01:04:21 UTC 2025


Following your idea, this is the final code:

import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;

public class Example {
    public static ThreadFactory threadFactory() {
        Semaphore one = new Semaphore(1);
        ThreadFactory threadFactory = Thread.ofVirtual().factory();

        return new ThreadFactory() {
            @Override
            public Thread newThread(Runnable runnable) {
                Runnable newRunnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            one.acquire();
                            runnable.run();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } finally {
                            one.release();
                        }
                    }
                };

                return threadFactory.newThread(newRunnable);
            }
        };
    }
}

It works fine, however, would be great if the structured concurrency api
provides a better option, but I understand if this is case is not that
broadly used.

Thanks for the help.

Daniel
e. estigma88 at gmail.com

On Wed, Mar 26, 2025, 9:29 AM Daniel Andres Pelaez Lopez <
estigma88 at gmail.com> wrote:

> I did try to inherit from StructuredTaskScope and create our
> implementation:
>
> public class SameThread extends StructuredTaskScope<Object> {
>     @Override
>     public <U> Subtask<U> fork(Callable<? extends U> task) {
>         try {
>             return new SameThreadTask(task);
>         } catch (Exception e) {
>             throw new RuntimeException(e);
>         }
>     }
> }
>
> class SameThreadTask<T> implements StructuredTaskScope.Subtask<Object> {
>     private Callable<Object> callable;
>
>     public SameThreadTask(Callable<Object> callable) {
>         this.callable = callable;
>     }
>
>     @Override
>     public Callable<?> task() {
>         return callable;
>     }
>
>     @Override
>     public State state() {
>         return null;
>     }
>
>     @Override
>     public Object get() {
>         try {
>             return callable.call();
>         } catch (Exception e) {
>             throw new RuntimeException(e);
>         }
>     }
>
>     @Override
>     public Throwable exception() {
>         return null;
>     }
> }
>
> However, StructuredTaskScope.Subtask is sealed, so, we cannot inherit from
> it
>
> > A ThreadFactory can decorate the
> > Runnable to use a Semaphore to limit concurrency and a single permit
> > would ensure the subtasks execute sequentially
>
> I will try that, however, seems quite complex anyway.
>
> El mié, 26 mar 2025 a las 3:37, Alan Bateman (<alan.bateman at oracle.com>)
> escribió:
>
>> On 25/03/2025 20:35, Daniel Andres Pelaez Lopez wrote:
>> >
>> > :
>> >
>> > Challenge:
>> >
>> > Testing JEP 499: Structured Concurrency (Third Preview) we couldn't
>> > find an easy way to do this, perhaps we need to create a new
>> > StructuredTaskScope implementation as we do with Executor interface?
>> > or should we pass a custom ThreadFactory that only creates one thread?
>>
>> A new virtual thread is started to execute each subtask. So no
>> equivalent of a "caller runs" policy. A ThreadFactory can decorate the
>> Runnable to use a Semaphore to limit concurrency and a single permit
>> would ensure the subtasks execute sequentially. Would that help what you
>> are doing?
>>
>> -Alan
>>
>
>
> --
> Daniel Andrés Pelaez López
> Master’s Degree in IT Architectures, Universidad de los Andes.
> Software Construction Specialist, Universidad de los Andes.
> Bachelor's Degree in Computer Sciences, Universidad del Quindio.
> e. estigma88 at gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250410/cb7d48d2/attachment-0001.htm>


More information about the loom-dev mailing list