Run simultaneous java threads with parameters, run method

Asked

Viewed 563 times

1

I have a service today that I need to calculate its running time with multiple accesses, so I’m trying to run simultaneous threads and log that time, the problem is that for the run method of the Thread class I cannot pass parameters and in case I need these parameters to run the service. How can I do that? Am I on the right track? Follow 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();
}
  • It’s easy to pass parameters to a Thread or Runnable that you pass to the Thread constructor. Just create a class that implements Runnable or extends Thread and pass the parameters in the constructor. Now as for your test strategy, I can’t tell if you’re on the right track because I understand that each request is already handled by a separate thread in your container Rvlet. And you’re looking to pass the request and answer objects to new threads instead of measuring Servlet’s time... you know what I mean? But I can’t give too much pitaco because I didn’t quite understand the intention.

  • (and also because I don’t know much of web programming heheh)

  • in case I want to simulate as if there were 5 accesses to the service during the same period and how long it took each of these executions, are already separate requests due to Servlet, but I can not do this simultaneous time simulation

  • Got it. What you really want to test are the calls to executeCommand. Then do as you are in @Dudaskank’s reply.

1 answer

4


Unable to pass parameters to the method run(), but you can create a class that implements Runnable, or extension 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.

Another thing, your exception FactoryException you 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.