Run simultaneous threads in java with parameters, run method

I have a service today that I need to calculate its execution time with multiple accesses, for this I am trying to run simultaneous threads and that log this time, the problem is that for the run method of The Thread class I can not pass parameters and in case I need these parameters for execution of the service. How can I do that? Am I on the right track? Following 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();
}
Author: Stenio Vilar, 2018-03-14

1 answers

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

Another Thing, Your exception FactoryException won't be able to use it that way either. You can't change the method signature, so you'll have to treat it inside the thread.

In your example, it would look something 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();
 4
Author: Dudaskank, 2018-03-14 19:37:48