简体   繁体   中英

How to create two instances of the same method with different parameters

I need to create 2 instances that run the same SQL procedure but with different parameters.

public void run() {

    // TRUE if there is no more VER_STOCK
    boolean booEsgotado = false;
    System.out.println("Starting thread" + numThread );
    try {
        objLigacao = DriverManager.getConnection(LIGACAO,
                UTILIZADOR, SENHA);
        // manual control of transactions
        objLigacao.setAutoCommit(false);

        while (booEsgotado == false && i<=5) {

            try {

                objComando = objLigacao.prepareCall(INSERE);

                // 1 = first parameter (:1)
                objComando.setInt(1, ID);
                objComando.setInt(2, PRODUTO);
                objComando.setInt(3, Q);


                objComando.execute();
                objComando.close();
                // If done with success commit the operations
                objLigacao.commit();
                i++;
                System.out.println("Sold a unit in thread " + numThread + " i = " + i);


                objComando = objLigacao.prepareCall(QUANT);
                objComando.setInt(1, PRODUTO);
                objResultado = objComando.executeQuery();
                while(objResultado.next()) {
                stock=objResultado.getInt(1);}
                System.out.println("Stock atual=" + stock);


            }
            catch (SQLException objExcepcao) {

                System.out.println("" + objExcepcao.getMessage());
                // If something failed rollback the operations

                objComando.close();
                objLigacao.rollback();
                booEsgotado = true;
                System.out.println("Product is out of stock in thread" + numThread);
            }
        }
        // Libertação de recursos.
        objLigacao.close();
    } catch (SQLException objExcepcao) {
        System.out.println(objExcepcao.getMessage());
    }

    System.out.println("The end of thread " + numThread );

}

The thing is that I can only run the same procedure with the same arguments in both instances. Where I need to execute the same procedure but with different arguments in both instances.

runne1 objInstancia1 = new runne1(1);
 runne1 objInstancia2 = new runne1(2);
 // Create a thread for each instance
 Thread objThread1 = new Thread(objInstancia1);
 Thread objThread2 = new Thread(objInstancia2);
 objThread1.start();
 objThread2.start();
 try {
 objThread1.join();
 objThread2.join();

Subclass Thread to provide a constructor that specifies parameters you need and store them as instance fields.
In this way, you could use them in the run() method.

public class MyCallThread extends Thread {

   private int paramOne;
   private int paramTwo;

   public MyCallThread (Runnable runnable, int paramOne, int paramTwo){
     super(runnable);
     this.paramOne = paramOne;
     this.paramTwo = paramTwo;
   }

   public void run(){
       ... 
       objComando.setInt(1, paramOne);
       ...

   }
}

And instantiate it like that :

 int paramOne = ...;
 int paramTwo = ...;
 Thread objThread1 = new MyCallThread(objInstancia1, paramOne, paramTwo);
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM