存储当前运行的线程,然后返回这些值

时间:2017-10-03 18:22:50

标签: java multithreading selenium

我希望实现以下功能 - 用户指定同时运行的线程数和带有帐户列表的csv文件。它逐个通过每个帐户,并对每个帐户执行可运行的操作。我要做的是跟踪当前运行的线程数,以便我可以使用特定的exe运行每个线程。因此,如果我同时运行5个线程,它将能够使用driver1.exe,driver2.exe,driver3.exe,driver4.exe,driver5.exe。下面是我如何处理抓取数据并将其传递给线程。

public class MoveSpam 
{
    static int j = 0;
    public static boolean maxThreadsWait = false;
    public static int currentThreads = 0;
    public static int threadnum = 0;

    //static int numthr = ;

    @SuppressWarnings("unused")
    public static void main(String[] args) 
    {

        String username = null;  
        String password = null;
        String recoveryEmail = null;
        String recoveryPhone = null;
        String cookie = null;
        int localthread = 0;

        //System.out.println(System.getProperty("user.dir/required/"));
        // System.out.print();
        int threadCount = Integer.parseInt(args[1]);
        ArrayList<Thread> threads = new ArrayList<Thread>();
        AccountData accountData = new AccountData();
        accountData.loadDataFromFile((args[0]));
        System.out.println("Number of Accounts: "+accountData.getNumberOfAccounts());
        int vals = accountData.getNumberOfAccounts();

        final int numThreads = 5;
       // ExecutorService exec = Executors.newFixedThreadPool(numThreads);
        //ExecutorService executor = Executors.newCachedThreadPool();

        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        for( j = 0; j <vals; j++ )
        {

        int randomNum = ThreadLocalRandom.current().nextInt(0, vals );
        String accountInfo = accountData.getAccountNumber(randomNum).toString();
        String[] currentAccount;
        String delimiter = ",";
        currentAccount = accountInfo.split(delimiter);
        username = currentAccount[0];   
        password = currentAccount[1];
        recoveryEmail = currentAccount[2];
        recoveryPhone = currentAccount[3];
        cookie = currentAccount[4];
        System.out.println("Processing Account: "+ currentAccount[0]+","+currentAccount[1]+","+currentAccount[2]+","+currentAccount[3]+","+currentAccount[4]);


        //ExecutorService executor = Executors.newCachedThreadPool();
        if (currentThreads < threadCount ){
            Runnable worker = new GmailMover(currentAccount[0],currentAccount[1], currentAccount[2], currentAccount[3], currentAccount[4], j);
            currentThreads ++;
            //threads.get(j).start();
            executor.execute(worker);


        }else{
            while (currentThreads >= threadCount){
                try {
                    System.out.println("Waiting 1000 MS");
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
            System.out.println("Spawning New Account");
            System.out.println("Number of threads ran:");

}

        }


        System.out.println("Cleared All inboxes! :)");  
    }
}

输入数字每次都会改变,所以不想为每个人设置一个案例,而是在另一个班级做以下事情。

public class GmailMover implements Runnable{
    WebDriver driver = null;
    String localusername = null;  
    String localpassword = null; 
    String localrecoveryemail = null; 
    String localrecoveryphone = null; 
    String localcookie = null; 
    int localthread = 0;
    boolean localTrigger = false;
    String status = null;
    public void run(){  
        System.out.print(localthread);
        String workingdir = System.getProperty("user.dir");
        workingdir = workingdir+"\\required";
        ///START
        String Browser = workingdir + "\\Selenium\\geckodriver"+localthread+".exe";
        System.out.println(Browser);
        System.setProperty("webdriver.gecko.driver", workingdir + "\\Selenium\\geckodriver"+localthread+".exe");//Set Browser

现在我已将它设置为将线程号传递给线程,但我无法弄清楚如何将其返回以便保留该值并且我可以使用返回的值启动另一个线程。所以一个例子是如果线程7完成(使用driver7.exe),下一个要启动的线程将被分配7,然后再次启动driver7.exe。任何指导将不胜感激!

0 个答案:

没有答案