vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.74k stars 194 forks source link

Make OpenHashSet a bit more concurrent friendly #306

Closed catap closed 7 months ago

catap commented 9 months ago

Here a quite trivial reorganization of OpenHashSet which allows it to be used on that way:

    public static void main(String[] args) {
        LongSet original = new LongOpenHashSet();
        LongSet readOnly = LongSets.unmodifiable(original);
        LongSet writable = LongSets.synchronize(original);

        SplittableRandom random = new SplittableRandom();

        AtomicBoolean failure = new AtomicBoolean();
        new Thread(() -> {
            while (true) {
                try {
                    readOnly.contains(random.nextLong());
                } catch (Throwable th) {
                    failure.set(true);
                    break;
                }
            }
        }).start();
        new Thread(() -> {
            while (!failure.get()) {
                writable.add(random.nextLong());
            }
        }).start();
    }

The goal is crash at OutOfMemory, and not because of concurent access.

vigna commented 7 months ago

Sorry, I really don't think it's a good idea.

catap commented 7 months ago

np, it is hack, yes.