-1

i am trying to invoke run() method of Runnable objects and i can't understand why it doesn't work. the purpose of this is to create set of various Runnable objects and add it Set, then to invoke all of them at once with their run() method. can you please help me to understand what am i doing wrong?

here is my code:

public class WebDriverThreadsManager 
{
    private int numOfFireFoxThreads = 0;

    private Set<Runnable> webDriversSet = new HashSet<Runnable>();


    public WebDriverThreadsManager(){}

    public void addWebDriversToSeperateThreadsSet(String i_NameOfBrowser) throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
         Class cls;
        switch (i_NameOfBrowser)
        {
            case "FireFox":
            {
                cls = Class.forName("SeleniumLerning.ThreadedFireFoxWebDriver");
                numOfInstancesToAddToThreadsSet(cls,this.getNumOfFireFoxThreads());
            }
            default:
            throw new IllegalStateException("Unknown Browser");
    }

    private void numOfInstancesToAddToThreadsSet(Class cls, int numOfInsances) throws InstantiationException, IllegalAccessException
    {
       for(int i=0 ; i < numOfInsances ; i++)
        {
            Object obj = cls.newInstance();
            this.webDriversSet.add((Runnable)obj);
        } 

    }

    public void invokeWebDrivers()
    {
       Iterator iter = this.webDriversSet.iterator();
       //Method method;

       while (iter.hasNext()) 
      {
         try 
         {
              Method  method = iter.getClass().getDeclaredMethod("run");
              method.invoke(iter.getClass());
         } 
         catch (NoSuchMethodException ex) 
        {
            Logger.getLogger(WebDriverThreadsManager.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (SecurityException ex) 
        {
            Logger.getLogger(WebDriverThreadsManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(WebDriverThreadsManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(WebDriverThreadsManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(WebDriverThreadsManager.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
}

}

public void Main()
  {
      public static void main(String args[])
      {
           WebDriverThreadsManager manager = new  WebDriverThreadsManager();
           manager.setNumOfFireFoxThreads(5);
           addWebDriversToSeperateThreadsSet("FireFox");
           invokeWebDrivers();
      }
 }

2 Answers2

0

I don't understand why you made them so complicated using Reflection, this can be done without using reflection.

But if for some reason you need to use reflection, this is probably what you need inside your try,

     try 
     {
          Object object = iter.next();
          Method  method = object.getClass().getDeclaredMethod("run");
          method.invoke(object);
     } 

Note: you had also mentioned,

then to invoke all of them at once

But this will only call them one by one, you are not creating multiple threads here.

Codebender
  • 14,221
  • 7
  • 48
  • 85
0

You really don't need reflection here. You have a nicely typed Set<Runnable>, why not take advantage of it, like this:

// No messy try-catch blocks required anymore :)
for(Runnable runnable : this.webDriversSet) {
    runnable.run();
}

However, you should also know that calling run() directly on a runnable will not automatically execute the list of runnables on different threads, they will just run sequentially on the current thread.

Read here to understand why this is: Java: What's the difference between Thread start() and Runnable run().

If your intent is to start all the runnables on different threads so that they run in parallel, then you could do it like this:

for(Runnable runnable : webDriversSet) {
    new Thread(runnable).start();
}
Community
  • 1
  • 1
sstan
  • 35,425
  • 6
  • 48
  • 66