thunkware / virtual-threads-bridge

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

Add api to create ThreadFactory #4

Closed JordiMartinezVicent closed 10 months ago

JordiMartinezVicent commented 10 months ago

Regarding the issue #3 I have creating this PR which adds the java 21 api to create Virtual Threads ThreadFactories.

For example, on java 21 it would be:

//VirtualThread factory
ThreadFactory virtualThreadFactory = Thread.ofVirtual()
        .name("name")
        .uncaughtExceptionHandler(ueh)
        .factory();

With this api:

//VirtualThread factory
 ThreadFactory virtualThreadFactory = ThreadFactoryTool.ofVirtual()
        .name("name")
        .uncaughtExceptionHandler(ueh)
        .factory();

In order to add the ThreadFactory with the current code:


ThreadFactory factory = ThreadFactoryTool.ofVirtual()
  .name("name-", 1)
   factory();

ExecutorService executor = ExecutorTool.newThreadPerTaskExecutor(factory);
  executor.submit(() -> {
    System.out.println(Thread.currentThread().getName()); // prints "name-1"
});
JordiMartinezVicent commented 10 months ago

@maxxedev

If you think is worthy to have the API, we can work on the PR to add it to the project.

maxxedev commented 10 months ago

@JordiMartinezVicent can you take a look at PR https://github.com/thunkware/virtual-threads-backport/pull/6

JordiMartinezVicent commented 10 months ago

@JordiMartinezVicent can you take a look at PR #6

Sure! I like it.

A part from the new api to create ThreadFactories I like the avoiding the reflection. Good job!

I close this PR in favor of #6