Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8351038: ConcurrentModificationException in EventType constructor #1729

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions modules/javafx.base/src/main/java/javafx/event/EventType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -64,8 +64,7 @@ public final class EventType<T extends Event> implements Serializable{
* indirect sub types of it. It is also the only event type which
* has its super event type set to {@code null}.
*/
public static final EventType<Event> ROOT =
new EventType<>("EVENT", null);
public static final EventType<Event> ROOT = new EventType<>("EVENT", true);

@SuppressWarnings("doclint:missing")
private WeakHashMap<EventType<? extends T>, Void> subTypes;
@@ -132,23 +131,11 @@ public EventType(final EventType<? super T> superType,
}

/**
* Internal constructor that skips various checks
* Internal constructor for the ROOT instance that skips various checks
*/
EventType(final String name,
final EventType<? super T> superType) {
this.superType = superType;
private EventType(String name, boolean ignored) {
this.superType = null;
this.name = name;
if (superType != null) {
if (superType.subTypes != null) {
for (Iterator i = superType.subTypes.keySet().iterator(); i.hasNext();) {
EventType t = (EventType) i.next();
if (name == null && t.name == null || (name != null && name.equals(t.name))) {
i.remove();
}
}
}
superType.register(this);
}
}

/**
@@ -179,7 +166,7 @@ public String toString() {
return (name != null) ? name : super.toString();
}

private void register(javafx.event.EventType<? extends T> subType) {
private synchronized void register(javafx.event.EventType<? extends T> subType) {
if (subTypes == null) {
subTypes = new WeakHashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package test.javafx.event;

import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javafx.event.Event;
import javafx.event.EventType;
import org.junit.jupiter.api.Test;

/**
* Developers should be able to create EventTypes in background thread(s).
*/
public class EventTypeConcurrencyTest {
@Test
public void concurrentInitialization() {
int N = 1_000;
try (var executor = Executors.newCachedThreadPool()) {
try {
ArrayList<Callable<Object>> runs = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
String name = "TEST" + i;
runs.add(() -> {
return new EventType<>(Event.ANY, name);
});
}

List<Future<Object>> futures = executor.invokeAll(runs);

for (Future<Object> future : futures) {
future.get();
}
} catch (Throwable e) {
fail(e);
}
}
}
}