News for hackers. Stuff that matters.
Re:Project Loom and Structured Concurrency by pron on Friday December 04, 2020 @09:30AM (#25302545)
Yes, and even on a per-thread basis.
Re:Project Loom and Structured Concurrency by ryanworl on Friday December 04, 2020 @09:38AM (#25302648)
Thanks! Is there an example "Hello World" executor that demonstrates the minimum functionality needed?

I would like an executor that runs all tasks on a single thread and schedules them deterministically for testing.

Re:Project Loom and Structured Concurrency by pron on Friday December 04, 2020 @09:49AM (#25302787)

    var myThreadFactory = Thread.builder().virtual(Executors.newSingleThreadExecutor()).factory();
All threads created by this thread factory will be scheduled to the same kernel thread.
Re:Project Loom and Structured Concurrency by Cojen on Friday December 04, 2020 @11:02AM (#25303792)
Does this example fully answer the question? Does this allow the tasks to be scheduled deterministically? If I'm very careful and write the tasks such that they make blocking calls at specific locations, then yes. Otherwise, is there any feedback to inform me that tasks are switching context at places I didn't expect? Is it possible to define a custom scheduler that can simply print debug messages at every context switch?
Re:Project Loom and Structured Concurrency by pron on Friday December 04, 2020 @11:13AM (#25303960)
It answers the question with a "hello, world." To do more sophisticated stuff, like what you want, you'll need to replace or wrap the standard single-thread Executor with your own Executor. There is exactly one method you need to implement. For example:

    Executor ste = Executors.newSingleThreadExecutor();
    Executor myExecutor = task -> {
      if (task instanceof Thread.VirtualThreadTask vtt) System.out.println("Scheduling " + vtt.thread() + " on " + Thread.currentThread());
      ste.execute(task);
      if (task instanceof Thread.VirtualThreadTask vtt) System.out.println("Descheduled " + vtt.thread() + " from " + Thread.currentThread());
    }
    var myThreadFactory = Thread.builder().virtual(myExecutor).factory();