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

link.jfire.testsupport.rule.MutiThreadStatement Maven / Gradle / Ivy

The newest version!
package link.jfire.testsupport.rule;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.runners.model.Statement;

public class MutiThreadStatement extends Statement
{
    private int                 repaetTimes;
    private int                 threadNums;
    private Statement           statement;
    private ExecutorService     pool;
    
    public MutiThreadStatement(int repaetTimes, int threadNums, Statement statement)
    {
        this.repaetTimes = repaetTimes;
        this.threadNums = threadNums;
        this.statement = statement;
        pool = Executors.newFixedThreadPool(threadNums);
    }
    
    public void evaluate() throws Throwable
    {
        List> set = new LinkedList<>();
        for (int i = 0; i < threadNums; i++)
        {
            Future future = pool.submit(new Callable() {
                
                public Void call() throws Exception
                {
                    try
                    {
                        for (int j = 0; j < repaetTimes; j++)
                        {
                            statement.evaluate();
                        }
                        return null;
                    }
                    catch (Throwable e)
                    {
                        throw new RuntimeException(e);
                    }
                }
            });
            set.add(future);
        }
        pool.shutdown();
        for (Future each : set)
        {
            each.get();
        }
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy