wait() in Java 24
Glavo
zjx001202 at gmail.com
Sat Nov 16 13:30:15 UTC 2024
This change is part of JEP 491[1], which was integrated[2] four days ago.
Glavo
[1]: https://openjdk.org/jeps/491
[2]:
https://github.com/openjdk/jdk/commit/78b80150e009745b8f28d36c3836f18ad0ca921f
On Sat, Nov 16, 2024 at 9:24 PM Dr Heinz M. Kabutz <heinz at javaspecialists.eu>
wrote:
> Hi,
>
> I was trying out Object.wait() on Java 24 to see what happens with
> virtual threads and it seems that it now works like ReentrantLock, but
> without resorting to ManagedBlockers.
>
> This code would not print anything prior to Java 24, but works fine now:
>
>
> import java.util.concurrent.*;
> import java.util.concurrent.atomic.*;
>
> public class ManyWaitingThreads {
> public static void main(String... args) throws InterruptedException {
> var counter = new AtomicInteger();
> var builder = Thread.ofVirtual();
> for (int i = 0; i < 100_000; i++) {
> builder.start(() -> {
> synchronized (ManyWaitingThreads.class) {
> try {
> counter.incrementAndGet();
> ManyWaitingThreads.class.wait();
> } catch (InterruptedException e) {
> throw new CancellationException("interrupted");
> }
> }
> });
> }
> builder.start(() -> System.out.println("All threads waiting"));
> while(counter.get() < 100_000) Thread.yield();
> synchronized (ManyWaitingThreads.class) {
> ManyWaitingThreads.class.notifyAll();
> }
> }
> }
>
> I seemed to have missed this new change, but it is very welcome. Thanks
> for all your hard work.
>
> Regards
>
> Heinz
> --
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java™ Specialists' Newsletter" - www.javaspecialists.eu
> Java Champion - www.javachampions.org
> JavaOne Rock Star Speaker
> Tel: +30 69 75 595 262
> Skype: kabutz
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20241116/7b98e1d5/attachment.htm>
More information about the loom-dev
mailing list