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

com.cloudhopper.commons.util.CountingRejectedExecutionHandler Maven / Gradle / Ivy

package com.cloudhopper.commons.util;

/*
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * 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.
 * #L%
 */

import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An implementation of a RejectedExecutionHandler that counts the number of
 * times it has been called.
 * 
 * @author joelauer
 */
public class CountingRejectedExecutionHandler implements RejectedExecutionHandler {
    private static final Logger logger = LoggerFactory.getLogger(CountingRejectedExecutionHandler.class);
    
    private AtomicLong rejected;
    
    public CountingRejectedExecutionHandler() {
        this.rejected = new AtomicLong();
    }
    
    public void reset() {
        this.rejected.set(0);
    }
    
    public long getRejected() {
        return this.rejected.get();
    }
    
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        this.rejected.incrementAndGet();
        throw new RejectedExecutionException();
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy