/*
 * @(#) StageThreadPoolExecutor.java
 * Created Dec 27, 2011 by oleg
 * (C) ONE, SIA
 */
package org.apache.cassandra.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * This pool executor is tied to its stage and launches tasks in current thread, if it belogs to
 * the neccessary stage.
 * 
 * @author Oleg Anastasyev<oa@hq.one.lv>
 *
 */
public class StageThreadPoolExecutor extends JMXConfigurableThreadPoolExecutor
{

    /**
     * @param corePoolSize
     * @param maximumPoolSize
     * @param keepAliveTime
     * @param unit
     * @param workQueue
     * @param name          stage name
     */
    StageThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
            long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue, String stageName)
    {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                new StageThreadFactory(stageName));
        
    }

    /* (non-Javadoc)
     * @see java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable)
     */
    @Override
    public void execute(Runnable runnable)
    {
        // running in the same thread, if current thread is of required stage
        if (Thread.currentThread() instanceof StageThread )
        {
            if ( ((StageThread)Thread.currentThread()).wasCreatedUsing(getThreadFactory()) )
            {
                runnable.run();
                return;
            }
        }
        
        super.execute(runnable);
    }
}

@@ -986,117 +986,8 @@
  %7D%0A%0A
-    /* (non-Javadoc)%0A     * @see java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable)%0A     */%0A
     
@@ -1427,11 +1427,198 @@
 );%0A    %7D
+%0A%0A    /**%0A     * @param forceFork true - force runnable to be processed by other thread%0A     */%0A    public void executeFork(Runnable runnable)%0A    %7B%0A        super.execute(runnable);%0A    %7D
 %0A%7D%0A

make quorum writes executed from writer thread to be executed by writer thread (OL-19378) + added ability to force fork of task to other thread due to