Example usage for java.util.concurrent RunnableFuture run

List of usage examples for java.util.concurrent RunnableFuture run

Introduction

In this page you can find the example usage for java.util.concurrent RunnableFuture run.

Prototype

void run();

Source Link

Document

Sets this Future to the result of its computation unless it has been cancelled.

Usage

From source file:net.dryuf.concurrent.benchmark.BenchmarkSupport.java

public static void threadedRunFutures(RunnableFuture<Integer>[] array) {
    Thread t = new Thread(() -> {
        for (RunnableFuture<Integer> v : array) {
            v.run();
        }//from  www  . ja  v  a2s. c o  m
    });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.flink.runtime.state.StateSnapshotCompressionTest.java

private void snapshotRestoreRoundtrip(boolean useCompression) throws Exception {

    ExecutionConfig executionConfig = new ExecutionConfig();
    executionConfig.setUseSnapshotCompression(useCompression);

    KeyedStateHandle stateHandle = null;

    ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("test", String.class);
    stateDescriptor.initializeSerializerUnlessSet(executionConfig);

    AbstractKeyedStateBackend<String> stateBackend = new HeapKeyedStateBackend<>(
            mock(TaskKvStateRegistry.class), StringSerializer.INSTANCE,
            StateSnapshotCompressionTest.class.getClassLoader(), 16, new KeyGroupRange(0, 15), true,
            executionConfig, TestLocalRecoveryConfig.disabled(), mock(HeapPriorityQueueSetFactory.class),
            TtlTimeProvider.DEFAULT);//w ww  . j  a  v  a 2 s  . c  o m

    try {

        InternalValueState<String, VoidNamespace, String> state = stateBackend
                .createInternalState(new VoidNamespaceSerializer(), stateDescriptor);

        stateBackend.setCurrentKey("A");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("42");
        stateBackend.setCurrentKey("B");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("43");
        stateBackend.setCurrentKey("C");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("44");
        stateBackend.setCurrentKey("D");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("45");
        CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(4 * 1024 * 1024);
        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot = stateBackend.snapshot(0L, 0L, streamFactory,
                CheckpointOptions.forCheckpointWithDefaultLocation());
        snapshot.run();
        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
        stateHandle = snapshotResult.getJobManagerOwnedSnapshot();

    } finally {
        IOUtils.closeQuietly(stateBackend);
        stateBackend.dispose();
    }

    executionConfig = new ExecutionConfig();

    stateBackend = new HeapKeyedStateBackend<>(mock(TaskKvStateRegistry.class), StringSerializer.INSTANCE,
            StateSnapshotCompressionTest.class.getClassLoader(), 16, new KeyGroupRange(0, 15), true,
            executionConfig, TestLocalRecoveryConfig.disabled(), mock(HeapPriorityQueueSetFactory.class),
            TtlTimeProvider.DEFAULT);
    try {

        stateBackend.restore(StateObjectCollection.singleton(stateHandle));

        InternalValueState<String, VoidNamespace, String> state = stateBackend
                .createInternalState(new VoidNamespaceSerializer(), stateDescriptor);

        stateBackend.setCurrentKey("A");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("42", state.value());
        stateBackend.setCurrentKey("B");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("43", state.value());
        stateBackend.setCurrentKey("C");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("44", state.value());
        stateBackend.setCurrentKey("D");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("45", state.value());

    } finally {
        IOUtils.closeQuietly(stateBackend);
        stateBackend.dispose();
    }
}