All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jpower.core.WorkerPool Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
package jpower.core;

import jpower.core.utils.ThreadUtils;

import java.util.ArrayList;
import java.util.List;

public class WorkerPool
{

    private final List workers = new ArrayList<>();
    private final int size;

    public WorkerPool()
    {
        this(50);
    }

    public WorkerPool(int size)
    {
        this.size = size;
    }

    public boolean submit(Task task)
    {
        Worker worker = pullWorker();
        if (worker != null)
        {
            worker.addTask(task);
            return true;
        }
        return false;
    }

    private Worker pullWorker()
    {
        if (workers.isEmpty())
        {
            Worker worker = new Worker();
            workers.add(worker);
            return worker;
        }
        for (Worker w : workers)
        {
            if (!w.isWorking())
            {
                return w;
            }
        }
        return newWorker();
    }

    private Worker newWorker()
    {
        if (workers.size() == size)
        {
            return null;
        }
        Worker worker = new Worker();
        worker.start();
        workers.add(worker);
        return worker;
    }

    public List getWorkers()
    {
        return workers;
    }

    public void stopWorkers()
    {
        Iterable temp = new ArrayList<>(workers);
        temp.forEach(worker -> {
            worker.stop();
            new ConditionalExecutor(() -> ThreadUtils.sleep(1)).until(worker::isWorking);
            workers.remove(worker);
        });
    }

    public int getWorkerCount()
    {
        return workers.size();
    }

    public void waitForAll()
    {
        workers.forEach(Worker::waitFor);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy