new Blocking Executors - Java java.util.concurrent

Java examples for java.util.concurrent:Executors

Description

new Blocking Executors

Demo Code


import org.apache.log4j.Logger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main{
    private static Logger LOGGER = Logger.getLogger(ExecutorUtils.class);
    /**// w  w w .j a v  a  2s  .  c  o  m
     * @param size
     * @return
     */
    public static ExecutorService newBlockingExecutors(int size) {
        return new ThreadPoolExecutor(size, size, 0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1) {
                    /**
                     *
                     */
                    private static final long serialVersionUID = 1L;

                    /**
                     * (non-Jsdoc)
                     *
                     * @see java.util.concurrent.LinkedBlockingQueue#offer(java.lang.Object)
                     */
                    @Override
                    public boolean offer(Runnable e) {
                        try {
                            this.put(e);
                        } catch (Exception e1) {
                            LOGGER.warn("offer error ", e1);
                        }
                        return true;
                    }
                });
    }
}

Related Tutorials