thunkware / virtual-threads-bridge

Use Java21's Virtual Threads API in Java 8+
MIT License
6 stars 3 forks source link

`SemaphoreExecutor` fails to execute `Runnable` tasks #26

Closed dhoard closed 1 month ago

dhoard commented 1 month ago

Summary

SemaphoreExecutor fails to execute a Runnable tasks

Expected Output

10 x Task is running System.out prints 10 x Task completed System.out prints

Actual Output

no System.out prints

Test Code

import io.github.thunkware.vt.bridge.ExecutorTool;
import io.github.thunkware.vt.bridge.SemaphoreExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class ExecutorServiceExample {

    public static void main(String[] args) {
        ExecutorService executor = new SemaphoreExecutor(ExecutorTool.newVirtualThreadPerTaskExecutor(), 2);
        List<Future<?>> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Future<?> future = executor.submit(new RunnableTask());
            futures.add(future);
        }

        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }

        executor.shutdown();
    }

    private static class RunnableTask implements Runnable {

        @Override
        public void run() {
            System.out.println("Task is running");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            System.out.println("Task completed");
        }
    }
}
maxxedev commented 1 month ago

Embarrassing bug. Fixed. Thanks!