Problem with java threads

1

I'm not sure how to get something like this to appear when I run:

  

Thread1 is running task 1
  The main (parent) thread has finished its execution
  Hilo1 is running task 2
  Hilo1 is executing task 3
  Hilo2 is running the task 1
  Hilo1 is running the task 4
  Thread1 is running task 5
  Hilo2 is running the task 2
  Hilo1 is running the task 6
  Hilo2 is running task 3
  Hilo1 is running the task 7
  Hilo2 is running the task 4
  Hilo1 is executing task 8
  Hilo2 is running the task 5
  Hilo1 is running the task 9
  Hilo1 is running the task 10
  Hilo1 has completed its execution
  Hilo2 is running the task 6
  Hilo2 is running the task 7
  Hilo2 is executing task 8
  Hilo2 is running the task 9
  Hilo2 is executing task 10

I have this structure but I'm not sure how to implement it.

public class Principal {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub




        HilosVarios hilo1 = new HilosVarios();

        HilosVarios2 hilo2 = new HilosVarios2(hilo1);
        hilo1.start();
        hilo2.start();
        System.out.println("Terminadas las tareas");
    }

}
    
asked by Juan 18.10.2018 в 13:20
source

1 answer

1

Here is an example of how you can create threads in different ways. Either extending the class Thread or implemented Runnable

Extending the Thread class ..

    class ThreadEjemplo extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Ejecutando Thread \'ThreadEjemplo\': " +i);
        }
    }

}

And we call it that ..

//Arranca un nuevo hilo. Clase ThreadEjemplo
new ThreadEjemplo().start();

Implemented Runnable ...

class RunnableEjemplo implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Ejecutando Thread \'RunnableEjemplo\': " +i);
        }
    }

}

And we call it that ..

new Thread(new RunnableEjemplo()).start();

Our class that implements Runnable is passed as a parameter to an instance of Thread

Example in full ..

public class Principal {
    public static void main(String[] args) {
        //Arranca un nuevo hilo. Clase ThreadEjemplo
        new ThreadEjemplo().start();

        //Arranca otro hilo con la clase que implementa Runnable
        new Thread(new RunnableEjemplo()).start();

        System.out.println("El hilo main (padre) ha terminado su ejecución");
    }
}

class ThreadEjemplo extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Ejecutando Thread \'ThreadEjemplo\': " +i);
        }
    }

}

class RunnableEjemplo implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Ejecutando Thread \'RunnableEjemplo\': " +i);
        }
    }

}

If you want to see it more closely when executing them, you can call the static sleep () - Thread.sleep(1); , within the respective for loop enclosing them within a block try - catch

@Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1);

            } catch (InterruptedException e) {
                System.out.println("El thread ha sido detenido");
            }

            System.out.println("Ejecutando Thread \'ThreadEjemplo\': " + i);
        }
    }
    
answered by 18.10.2018 / 14:43
source