Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Task in java with Threads

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Task in java with Threads

    Hi guys, I have two tasks, when I run the code normally and sequentially, the runtime is faster than 2 Threads which are handling my 2 tasks, why is happening this?, My Teacher told me that Dividing my tasks in Threads my code will run Faster than without 'em, But that is not Happing....Why??? Maybe the processor power is divided by two? Sorry About My English, I'm using translator... Bye


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Task in java with Threads

    Post here your code please. Maybe, there's some error, so we need to see.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Task in java with Threads

    package multi_matrices_hilos;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
     
    public class Multi_MAtrices_Hilos {    
     
        public static void main(String[] args) {
            GestorTareas gt = new GestorTareas();
            try{
                gt.Ciclo_Principal();
     
            }catch(Exception E){}
        }    
    }
     
    class GestorTareas {
        private Menu menu;
        private Multiplicar_Matriz datos;
     
        public GestorTareas(){
            menu = new Menu();
            datos  = new Multiplicar_Matriz(4000,4000,4000);
            menu = new Menu();
     
        }
     
        public void Ciclo_Principal() throws InterruptedException  
        {
     
            new Thread(datos).start();
            Thread.sleep(1);
            datos.set_numSubProceso((byte)1);        
            new Thread(datos).start();
            //datos.set_numSubProceso((byte)3);
     
            //datos.GenerarMatrizA();
            //datos.GenerarMatrizB();
     
            int op;
            try{
                op = menu.menu();
                gestor_opciones(op);
     
            }catch(Exception e){
                System.out.print("Error" + e.getMessage());
               // throw e;
            }
        }
        private void gestor_opciones(int op) throws IOException
        {
            switch(op)
            {
                case 1:
                    menu.datos_M_N_S();                
                    break;
                case 4:
                    break;
            }
        }    
    }
     
    class Menu 
    {
        private BufferedReader br;
     
        public Menu()
        {
            br = new BufferedReader( new InputStreamReader(System.in));
        }
        public int menu() throws IOException
        {
            System.out.println("1.Cargar Nuevos Matrices Datos.");
            System.out.println("2.Mostrar Matriz A.");
            System.out.println("3.Mostrar Matriz B.");
            System.out.println("4.Iniciar.");
            System.out.println("5.Salir.");
     
            return Integer.parseInt(br.readLine());
        }
        public int[] datos_M_N_S() throws IOException
        {
            int[] MNS = new int[3];
            System.out.print("\nInsertar M: ");
            Integer.parseInt(br.readLine());        
            System.out.print("\nInsertar N: ");
            Integer.parseInt(br.readLine());
            System.out.println("\nInsertar S: ");
            Integer.parseInt(br.readLine());
     
            return MNS;
        }
    }
     
     
    class Multiplicar_Matriz implements Runnable
    {
     
        @Override
        public void run() {
            switch(numSubProceso)
            {
                case 0:   
                    GenerarMatrizA();
                    break;
                case 1:   
                    GenerarMatrizB();
                    break;
                case 2:   
                    break;
            }        
     
        }
        public Multiplicar_Matriz()
        {
           numSubProceso = 0;
        }
        public Multiplicar_Matriz(int M,int N,int S)
        {
            this();
            this.M = M;this.N = N;this.S = S;
            MatrizA  = new int[M][N];
            MatrizB  = new int[N][S];
            Matriz_C = new int[M][S];        
        }
     
        public void GenerarMatrizA()
        {
            long tiempoInicio = System.currentTimeMillis();
            for(int i=0;i<M;i++)for(int j=0;j<N;j++) 
                MatrizA[i][j] = (int) Math.floor(Math.random()*(101));
     
            long totalTiempo = System.currentTimeMillis() - tiempoInicio;
     
            System.out.println("El Tiempo en generar la MatrizA es: "+totalTiempo);
        }
     
        public void GenerarMatrizB()
        {
            long tiempoInicio = System.currentTimeMillis();
            for(int i=0;i<N;i++)for(int j=0;j<S;j++) 
                MatrizB[i][j] = (int) Math.floor(Math.random()*(101));
     
            long totalTiempo = System.currentTimeMillis() - tiempoInicio;
     
            System.out.println("El Tiempo en generar la MatrizB es: "+totalTiempo);
        }
     
        public void multiplicarMatrices_A_B()
        {
            for (int k = 0; k < M; k++)for( int j = 0; j < S; j++)for(int i = 0; i < N; i++ )             
               Matriz_C[k][j] += MatrizA[k][i] * MatrizB[i][j];
        }
     
        public void mostrarMatricesC()
        {
            for(int i = 0; i < M; i++ ){for(int j = 0; j < S; j++)System.out.print(Matriz_C[i][j]+" ");
               System.out.println();
            }
        }
     
        public void set_numSubProceso(byte numSubProceso)
        {
            this.numSubProceso = numSubProceso;
        }
     
        private int M,N,S;
        private static int[][] Matriz_C;
        private int[][] MatrizA;
        private int[][] MatrizB;
        private byte numSubProceso;    
    }

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Task in java with Threads

    How do you measure time? I can't see corresponding idioms in your code.

Similar Threads

  1. [SOLVED] matrix task in java
    By mockingbird in forum What's Wrong With My Code?
    Replies: 16
    Last Post: May 14th, 2013, 04:14 PM
  2. pay task: java application.
    By chenaz in forum Paid Java Projects
    Replies: 0
    Last Post: July 11th, 2012, 09:54 AM
  3. how to implement Task Pane in Java
    By bashasmarty in forum Member Introductions
    Replies: 1
    Last Post: May 11th, 2012, 01:42 PM
  4. Replies: 5
    Last Post: September 26th, 2011, 12:54 PM
  5. test task for Junior java
    By umami in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 10:31 AM