
dorkbox.util.ParallelProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Utilities Show documentation
Show all versions of Utilities Show documentation
Utilities for use within Java projects
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
/**
* A Parallel processor to simplify processing data on multiple threads and provide back-pressure to the main thread (that
* creates the processor and adds work to it), so that memory is constrained at the expense of CPU waiting
*
* Remember that the JMM requires that empty 'synchronize' will not be optimized out by the compiler or JIT!
*
* This is NOT the FASTEST implementation, but it is relatively easy and solid.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract
class ParallelProcessor {
public
interface Worker {
/**
* Runs the work.
*
* @return true if there was work done, otherwise false.
*/
boolean process(Task objectToProcess);
}
private static final Object SENTINEL = new Object[0];
private final int numberOfThreads;
private final ArrayList threads;
private final ArrayBlockingQueue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy