Run concurrent threads in java with parameters, run method

1

I have a service today that I need to calculate its execution time with multiple accesses, for this I am trying to execute concurrent threads and that loguem this time, the problem is that for the run method of the Thread class I can not pass parameters and in case I I need these parameters to execute the service. How can I do this? I'm in the right way? Here is the code:

for(int x = 0; x<50 ; x++){
    new Thread() {
       long tempInicial = System.currentTimeMillis();
       @Override
        public void run(HttpServletRequest request,
                HttpServletResponse response, String sequence,
                CommandMapping commMapping, CommandForm form) throws FactoryException { //ERRO -> Não posso ter esses parâmetros

                Command command = CommandFactory.getInstance().getCommand(
                    commMapping.getCommandPath());
                CommandResponse commResponse = null;
                try {
                    commResponse = command.executeCommand(form, request, response, commMapping.isValidateForm());
                } catch (ServletException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                long tempFinal = System.currentTimeMillis();
                long dif = (tempFinal - tempInicial);
                log.info("Requisição " + ": " + String.format("%02d segundos  e %02d milisegundos", dif/60, dif%60));

        }

    }.start();
}
    
asked by anonymous 14.03.2018 / 20:21

1 answer

4

You can not pass parameters to the run() method, however you can create a class that implements Runnable , or extend Thread , and in the constructor (or via get / set methods or whatever creativity allows) pass the parameters that will be used and save in the attributes of your class.

Otherwise, your FactoryException exception will not be able to use it that way either. You can not change the method signature, so you have to treat it within the thread.

In your example, it looks more or less like this:

public class MyRunnable implements Runnable {
  HttpServletRequest request;
  HttpServletResponse response;
  String sequence;
  CommandMapping commMapping;
  CommandForm form;
  long tempInicial = System.currentTimeMillis();


  public MyRunnable(HttpServletRequest request, HttpServletResponse response, String sequence, CommandMapping commMapping, CommandForm form) {
    this.request = request;
    this.response = response;
    this.sequence = sequence;
    this.commMapping = commMapping;
    this.form = form;
  }

  public void run() {
    // aqui pode usar normalmente os parâmetros agora
    try {
      Command command = CommandFactory.getInstance().getCommand(commMapping.getCommandPath());
      CommandResponse commResponse = null;
      try {
        commResponse = command.executeCommand(form, request, response, commMapping.isValidateForm());
      } catch (ServletException e) {
        e.printStackTrace();
      }

      long tempFinal = System.currentTimeMillis();
      long dif = (tempFinal - tempInicial);
      log.info("Requisição " + ": " + String.format("%02d segundos  e %02d milisegundos", dif/60, dif%60));
    } catch(FactoryException fe) {
      fe.printStackTrace();
    }
  }
}

And to create and run the thread:

Runnable r = new MyRunnable(request, response, sequence, commMapping, form);
new Thread(r).start();
    
14.03.2018 / 20:37