【问题标题】:How to use threadpool executor in java to run the single java application twice simultaneously如何在java中使用线程池执行器同时运行单个java应用程序两次
【发布时间】:2013-12-15 14:55:41
【问题描述】:

实际上,我正在尝试运行 selenium 框架以进行 Web 应用程序测试。 我能够使用 excel 表格数据作为输入来运行框架。当我尝试使用不同的输入运行它时,我的框架应该在本地系统中同时使用不同的输入运行。我认为这可以在 Threadpool executor 中完成。 我可以为两组输入启动两个线程。但是当第二个线程启动时,第一个线程死了。但是在使用单个输入运行时,它运行良好。我不知道我的代码中缺少什么。我已经给出了下面的代码, 请朋友们看看,

Driverscript.java

public static void main (String[] args) throws ReflectiveOperationException, Exception {
        String configPath =args[0];
        DriverScript Test = new DriverScript(configPath);

        //Test.start(configPath);
        String mapFile=CONFIG.getProperty("Application");
        Xls_Reader ApplicationXLS=new Xls_Reader(mapFile);
        int rowCount=ApplicationXLS.getRowCount(Constants.TEST_APP_SHEET);
        ExecutorService executor =Executors.newFixedThreadPool(rowCount);
        for(int CurrentApplicationID=2;CurrentApplicationID<=ApplicationXLS.getRowCount(Constants.TEST_APP_SHEET);CurrentApplicationID++){          
            if(ApplicationXLS.getCellData(Constants.TEST_APP_SHEET, Constants.TEST_APP_RUNMODE,CurrentApplicationID).equalsIgnoreCase("Permitted")){    
                SuitePath=ApplicationXLS.getCellData(Constants.TEST_APP_SHEET, Constants.TEST_APP_SUITEPATH,CurrentApplicationID);
                ObjectRepo=ApplicationXLS.getCellData(Constants.TEST_APP_SHEET, Constants.TEST_APP_OBJECT_REPOSITORY,CurrentApplicationID);
                new ObjectRepLocator(ObjectRepo);
                SuiteXLS  = new Xls_Reader(SuitePath);
                CurrentSuiteSheet=ApplicationXLS.getCellData(Constants.TEST_APP_SHEET, Constants.TEST_APP_ID, CurrentApplicationID);
                Runnable worker=new ThreadScheduler(SuiteXLS,CurrentSuiteSheet);
                Thread.currentThread().join(3000);
                executor.execute(worker);

             }
            }
        executor.shutdown();
        if(executor.isTerminated())
        new ReportUtil(configPath,SuitePath);
        }

    public static void start(Xls_Reader SuiteXLS,String CurrentSuiteSheet) throws ReflectiveOperationException, IllegalArgumentException, Exception {

    <<----content of the Start() function---->>

}

ThreadScheduler.java

public class ThreadScheduler implements Runnable {
    public static Xls_Reader SuiteXLS;
    public static String currentSuiteSheet;
    public static String currentTestSuitePath;
    ThreadScheduler(Xls_Reader SuiteXLS, String CurrentSuiteSheet){
        this.SuiteXLS=SuiteXLS;
        this.currentSuiteSheet=CurrentSuiteSheet;
    //  this.currentTestSuitePath=CurrentTestSuitePath;
    }
    @Override
    public void run(){
        try{
            System.out.println(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getState());
            System.out.println(SuiteXLS);
            System.out.println(currentSuiteSheet);  
        DriverScript.start(SuiteXLS,currentSuiteSheet);
        System.out.println("End of "+ Thread.currentThread().getName());

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

我正在使用 Eclispe juno。该框架用于 Selenium 自动化。 在此先感谢朋友...

【问题讨论】:

    标签: java multithreading selenium threadpool threadpoolexecutor


    【解决方案1】:

    您正在调用“executor.shutdown();”这就是为什么你的线程死了,否则它会在线程池中等待任务。就您的问题而言,您需要一种重复任务,这可以通过使用诸如石英计划之类的东西来实现,其中任务可以配置为以独立的重复间隔并行运行。

    【讨论】:

      猜你喜欢
      • 2014-08-27
      • 2017-12-29
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多