I throw an executor but only one thread is executed

0

My question was that when trying to do a function with an executor (in this case, a multiplication of matrices) and when checking the thread that was in the run, it always came out the same, 12.

/*
Atributos de la clase
*/
    public int x,y;
    public  int matriz2[][];
    public  int matres[][];
    public int mattriz1[][];

    /*Constructor de la clase*/
    public prodMatConcurrente(int [][] mat1,int [][] mat2,int[][] matres,int x){
        this.x=x;
        this.mattriz1 = mat1;
        this.matriz2 = mat2;
        this.matres = matres;
    }



    public void run(){
       for(int i = 0;i<matriz2.length;i++){
            for(int j = 0;j<matriz2.length;j++){
                matres[x][i]=matres[x][i] + mattriz1[x][j] * matriz2[j][i];
            }
        }
        long threadId = prodMatConcurrente.currentThread().getId();
          System.out.println("I am thread " + threadId);
    }

    public static void main(String[] args) throws Exception{

        double tiempo_inicial = 0;
        int nNuc = Runtime.getRuntime().availableProcessors();
        float Cb = Float.parseFloat(args[0]);
        int tamPool = (int)(nNuc/(1-Cb));
        System.out.println("El numero de hilos es:" +tamPool);

        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        System.out.println("Introduzca la primera dimension de la matriz");
        int x = sc.nextInt();
        System.out.println("Introduzca la segunda dimension de la matriz");
        int y = sc.nextInt();
        int mat[][] = new int[x][y];
        int mat2[][] = new int[x] [y];
        int matres[][] = new int [x][y];
        //Inicializamos la matriz resultado
        for(int i = 0; i<x;i++){
            for(int j =0;j<y;j++){
                matres[i][j] = 0;
                }
            }
        //Rellenamos la matriz con random
      for(int i = 0; i<x;i++){
            for(int j =0;j<y;j++){
                mat[i][j] = rand.nextInt(10)+1;
                }
            }
       //imprime la primera matriz
        /*for(int i = 0;i<x;i++){
            for(int j = 0;j<y;j++){
                System.out.print(" "+mat[i][j]+" ");
            }
        System.out.println();
        }*/
        System.out.println("-----------------------------------");
        //Rellenamos la segunda matriz
         for(int i = 0; i<x;i++){
            for(int j = 0;j<y;j++){
                mat2[i][j] = rand.nextInt(10)+1;
            }
        }
        /*for(int i = 0;i<x;i++){
            for(int j = 0;j<y;j++){
                System.out.print(" "+mat2[i][j]+" ");
            }
        System.out.println();
        }*/
        System.out.println("-----------------------------------");
        prodMatConcurrente [][] h = new prodMatConcurrente[x][y];

        int tamPool2=Math.round(x/tamPool);
        tiempo_inicial = System.nanoTime();
        //Ejecutor que tiene de tamaño la dimensión de la fila de la matriz  
        //entre el resultado de la ecuación de Subramanian
        ExecutorService exe= Executors.newFixedThreadPool(tamPool2);
        for(int i=0;i<x;i++){
            exe.execute(new prodMatConcurrente(mat,mat2,matres,i));
        }


        exe.shutdown();
        while(!exe.isTerminated()){}
        //exe.awaitTermination(100,TimeUnit.NANOSECONDS);
        long finTiempo = System.nanoTime();
        System.out.println("El tiempo que ha tardado en ejecutrase es  "+(finTiempo-tiempo_inicial)/1.0e9);
        /*for(int i = 0;i<x;i++){
            for(int j = 0;j<y;j++){
                System.out.print(" "+matres[i][j]+" ");
            }
        System.out.println();
        }*/
    }   
}

The minimum number of threads you create when doing the Subramian equation with arg [0] = 0 is 8, so for an 8x8 matrix it would have to be 1 thread per row or so it should be.

    
asked by Resco 24.11.2017 в 14:24
source

1 answer

0

With the logic you have now, if Runtime.getRuntime().availableProcessors() returns 5 processors or more (very likely), this will result in you creating a thread pool with a single thread:

ExecutorService exe= Executors.newFixedThreadPool(tamPool2); // tamPool2 == 1?

Ironically, if you have 4 processors or less, then your thread pool will create more than one thread, and the work will be done in parallel, but this does not seem to be your case.

If we follow your logic in stages assuming 8 processors in your machine for taking an example:

  • Based on what you mention, let's say that args[0] == "0" and x == 8 (the dimension of the matrix.
  • Cb = 0 ( float Cb = Float.parseFloat(args[0]); ).
  • nNuc = 8 ( int nNuc = Runtime.getRuntime().availableProcessors(); )
  • tamPool = 8 ( int tamPool = (int)(nNuc/(1-Cb)); // 8/(1-0) )
  • tamPool2 = 1 !!! = int tamPool2=Math.round(x/tamPool); // 8/8
  • And since tamPool2 is the variable you use to set the size of the thread pool, you end up with just one thread.

    The truth is that I do not understand your logic. But if your intention is that you want each row of the matrix to be processed in parallel, then you need the thread pool to have at least x threads ( x being the number of rows in your matrix).

    So instead of using tamPool2 to create the thread pool, which seems to be wrong, simply use x :

    ExecutorService exe= Executors.newFixedThreadPool(x); // x == 8
    
        
    answered by 24.11.2017 / 16:10
    source