KivaKit-Schnipsel Nr. 6

Jan 03 2023
Mehr über KivaKit https://state-of-the-art.org/https://www.
Code

package com.telenav.kivakit.snippets;

import com.telenav.kivakit.core.collections.list.ObjectList;
import com.telenav.kivakit.core.language.trait.TryTrait;
import com.telenav.kivakit.core.thread.ThreadLocalMap;
import com.telenav.kivakit.core.value.count.Count;
import com.telenav.kivakit.core.value.count.MutableCount;

import java.util.concurrent.Future;

import static com.telenav.kivakit.core.collections.list.ObjectList.list;
import static com.telenav.kivakit.core.ensure.Ensure.ensureEqual;
import static com.telenav.kivakit.core.thread.Threads.shutdownAndAwaitTermination;
import static com.telenav.kivakit.core.thread.Threads.threadPool;
import static com.telenav.kivakit.core.value.count.Count._10_000;
import static com.telenav.kivakit.core.value.count.Count._8;

public class Snippet6 extends BaseSnippet implements TryTrait
{
    public static void main(String[] args)
    {
        new Snippet6().run();
    }

    @Override
    protected Object onSnippet()
    {
        var threads = _8;
        var iterations = _10_000;

        var map = new ThreadLocalMap<Integer, Integer>();

        var executor = threadPool("Snippet-6", threads);
        ObjectList<Future<Count>> futures = list();
        threads.loop(() -> futures.add(executor.submit(() ->
        {
            iterations.forEachInteger(i -> map.put(i, i));
            iterations.forEachInteger(i -> ensureEqual(i, map.get(i)));
            return iterations;
        })));

        var total = new MutableCount();
        for (var future : futures)
        {
            total.plus(tryCatch(() -> future.get()));
        }

        shutdownAndAwaitTermination(executor);
        return total;
    }
}

      
                
Output
80,000

Mehr über KivaKit

https://state-of-the-art.org/
https://www.kivakit.org/

Zum Thema passende Artikel

KivaKit-Schnipsel Nr. 1
KivaKit-Schnipsel Nr. 2
KivaKit-Schnipsel Nr. 3

Sechs Gründe, warum das meiste Java, das Sie gesehen haben, kein echtes Java
ist Warum ein Host kein String und ein Port keine ganze
Zahl ist eine ganze Zahl keine Zahl ist
Alles ist Teile und Herrsche